I made this plot using the following code:
ggplot(all, aes(x = year, color = layer))
geom_histogram(binwidth = 0.5, fill = "white", alpha = 0.5, position = "dodge")
scale_x_continuous(breaks = pretty(all$year))
scale_color_discrete(name = "title", labels = c("A","B"))
theme_light()
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
text = element_text(size = 20),
axis.title.x = element_text(margin = margin(t = 25, r = 0, b = 0, l = 0)),
axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),
axis.text.x = element_text(angle = 50, hjust = 1, size = 18, color = "black"),
axis.text.y = element_text(size = 18, color = "black"))
I would now like to change the colors first, using colors from the viridis palette. Furthermore, there are blue and red strokes between the histograms, which I would like to remove. Could someone help me to change the code? Thanks in advance!
Test Data:
year <- runif(10, 2014, 2021)
year <- round(year, 0)
layer <- sample(c("A","B"), size=10, replace=T)
all <- as.data.frame(year,layer)
CodePudding user response:
Seems like you want a bar plot not a histogram.
all <- data.frame(year,layer) ## fix the sample data creation
ggplot(all, aes(x = year, fill = layer)) ## I think fill looks better...
geom_bar(position = position_dodge(preserve = "single")) ## bar, not histogram
#scale_x_continuous(breaks = pretty(all$year)) ## this line just confirmed defaults
scale_fill_viridis_d()
theme_light() ## omitted the rest of the theme as irrelevant for the issue at hand
If you do want outline color, not fill, switching to geom_bar
"fixes" the strokes between the bars:
ggplot(all, aes(x = year, color = layer))
geom_bar(position = position_dodge(preserve = "single"), fill = NA)
scale_color_viridis_d()
theme_light()
CodePudding user response:
Thank you, this is helpful information!