我使用以下代碼制作了這個圖:
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"))
我現在想先更改顏色,使用 viridis 調色板中的顏色。此外,直方圖之間還有藍色和紅色的筆劃,我想將其洗掉。有人可以幫我更改代碼嗎?提前致謝!
測驗資料:
year <- runif(10, 2014, 2021)
year <- round(year, 0)
layer <- sample(c("A","B"), size=10, replace=T)
all <- as.data.frame(year,layer)

uj5u.com熱心網友回復:
似乎您想要條形圖而不是直方圖。
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

如果您確實想要輪廓顏色而不是填充,請切換到geom_bar“修復”條之間的筆劃:
ggplot(all, aes(x = year, color = layer))
geom_bar(position = position_dodge(preserve = "single"), fill = NA)
scale_color_viridis_d()
theme_light()

uj5u.com熱心網友回復:
謝謝,這是有用的資訊!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/435050.html
