Reputation: 111
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")
I am not sure how to plot only the data that is included in a specific argument. Have anyone else experienced this?
Upvotes: 1
Views: 740
Reputation: 388982
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')
Upvotes: 1