1

I have a data frame that consists of Median, 1 and 3 percentile, ID, and group. Here is some test data, but real data consist of over 500 rows.

data <- data.frame( "County" = c(1,1,2,2,3,3), "Median" = c(5,7,8,2,4,5), "Low" = c(0.5,2,4,1,2,3), "High" = c(10,12,11,9,10,15), "ID" = c("TRUE", "TRUE", "FALSE", "TRUE", "FALSE", "FALSE"), "group" = c(1,1,2,2,3,3))

I will always have a duplicate of "County" and in my plot, I want to show two geom_crossbars on top of each other (showing ID = TRUE/FALSE), SEE FIGURE. But now I would like to plot only the data that is included for group = 1, etc. And finally, I would like to get one plot for each group on a facet scheme.

This is my ggplot so far:

ggplot(data, aes(x = as.factor(County), 
                y = Median, 
                ymin = Low, 
                ymax = High)) +
  geom_crossbar(aes(color = ID, fill = ID, alpha = 1)) +
  scale_fill_manual(values = c("gray79", "brown4")) +
  scale_color_manual(values = c("gray50", "brown4")) +
  theme_classic() +
  scale_alpha(guide = "none") +
  xlab("County") +
  ylab("Variance") +
  labs(fill = "Model") +
  labs(color = "Model") 

enter image description here

I am not sure how to plot only the data that is included in a specific argument. Have anyone else experienced this?

2
  • + facet_wrap(~group, scales = 'free') ? Commented Feb 22, 2021 at 8:30
  • That was easy :) THANKS! Commented Feb 22, 2021 at 8:48

1 Answer 1

1

You can add facet_wrap/facet_grid to your code.

library(ggplot2)

ggplot(data, aes(x = as.factor(County), 
                 y = Median, 
                 ymin = Low, 
                 ymax = High)) +
  geom_crossbar(aes(color = ID, fill = ID, alpha = 1)) +
  scale_fill_manual(values = c("gray79", "brown4")) +
  scale_color_manual(values = c("gray50", "brown4")) +
  theme_classic() +
  scale_alpha(guide = "none") +
  xlab("County") +
  ylab("Variance") +
  labs(fill = "Model") +
  labs(color = "Model")  +
  facet_wrap(~group, scales = 'free')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.