[英]Overlaying barplot with line graphs using ggplot2
我正在ggplot
中創建一個圖形,其中我有一個條形圖,然后想要覆蓋多個折線圖。 出於這個問題的目的,我復制了兩個條形圖的代碼(一個包括所有年份(2007-2015)和兩個特定年份(2007年和2015年),但最終我將覆蓋10年不同的數據。使用的數據可以在這里找到。
library(dplyr)
library(tidyr)
library(gridExtra)
library(ggplot2)
overallpierc<-data[(data$item=="piercing"),]
overp<-overallpierc %>%
group_by(age) %>%
count(sex) %>%
ungroup %>%
mutate(age = factor(age)) %>%
complete(age, sex, fill = list(n = 0)) %>%
ggplot(aes(age, n)) + geom_col(aes(fill = sex), position = "dodge") +
theme_classic() +
scale_fill_manual(values=c("#000000", "#CCCCCC"), name = "Sex") +
labs(x = "Age", y = "Number of observations") +
theme(legend.position=c(0.4,0.8),
plot.title = element_text(size = 10),
legend.title=element_text(size=15),
axis.title=element_text(size=15),
legend.key.size = unit(1.13, "cm"),
legend.direction="vertical",
legend.text=element_text(size=15))
p07<-data[(data$yy=="2007") & (data$item=="piercing"),]
summary(p07)
subp07<-p07 %>%
group_by(age) %>%
count(sex) %>%
ungroup %>%
mutate(age = factor(age)) %>%
complete(age, sex, fill = list(n = 0)) %>%
ggplot(aes(age, n)) + geom_col(aes(fill = sex), position = "dodge") +
theme_classic() +
scale_fill_manual(values=c("#000000", "#CCCCCC"), name = "Sex") +
labs(x = "Age", y = "Number of observations") +
theme(legend.position=c(0.4,0.8),
plot.title = element_text(size = 10),
legend.title=element_text(size=15),
axis.title=element_text(size=15),
legend.key.size = unit(1.13, "cm"),
legend.direction="vertical",
legend.text=element_text(size=15))
p15<-data[(data$yy=="2015") & (data$item=="piercing"),]
subp15<-p15 %>%
group_by(age) %>%
count(sex) %>%
ungroup %>%
mutate(age = factor(age)) %>%
complete(age, sex, fill = list(n = 0)) %>%
ggplot(aes(age, n)) + geom_col(aes(fill = sex), position = "dodge") +
theme_classic() +
scale_fill_manual(values=c("#000000", "#CCCCCC"), name = "Sex") +
labs(x = "Age", y = "Number of observations") +
theme(legend.position=c(0.4,0.8),
plot.title = element_text(size = 10),
legend.title=element_text(size=15),
axis.title=element_text(size=15),
legend.key.size = unit(1.13, "cm"),
legend.direction="vertical",
legend.text=element_text(size=15))
grid.arrange(overp, subp07, subp15)
我想要做的是繪制2007年和2015年女性的頻率以及2007年和2015年的男性在總頻率的條形圖上的頻率(這也反映在圖例中)。 有沒有辦法在R
使用ggplot2
做到這ggplot2
?
更新:我嘗試使用geom_smooth
和geom_line
函數將這些行添加到我的ggplot
如評論中所建議的以及用戶問題的其他解決方案,但是我收到以下錯誤:
錯誤:提供給連續刻度的離散值
我為我要繪制的子集創建了一個新的數據框:
df<-data.frame(age=c(15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,40,50,60), val=c(0,5,13,77,70,106,62,51,46,27,46,16,22,16,14,48,21, 3,4))
然后將其添加到ggplot
代碼中:
overallpierc %>%
filter(age != "15") %>%
group_by(age) %>%
count(sex) %>%
ungroup %>%
mutate(age = factor(age)) %>%
complete(age, sex, fill = list(n = 0)) %>%
ggplot(aes(age, n)) +
geom_line(data=df,aes(x=as.numeric(age),y=val),colour="blue") +
geom_col(aes(fill = sex), position = "dodge") +
theme_classic() +
scale_fill_manual(values=c("#000000", "#CCCCCC"), name = "Sex") +
labs(x = "Age", y = "Number of observations") +
theme(legend.position=c(0.4,0.8),
plot.title = element_text(size = 10),
legend.title=element_text(size=15),
axis.title=element_text(size=15),
legend.key.size = unit(1.13, "cm"),
legend.direction="vertical",
legend.text=element_text(size=15))
其他人遇到過類似的問題,並使用as.numeric
來解決問題。 但是,年齡需要被視為繪圖目的的一個因素。
根據我們在評論中的討論,讓我們嘗試堆疊的條形和方面。 我認為它有效,但你可以自己決定。
堆疊條的優點是在同一條中顯示比例和總數。 為了比較年份,刻面網格在行中放置多年,因此眼睛可以向下掃描以比較不同年份的相同年齡。 請注意,我在這里將年齡保持為連續變量,而不是一個因素。
library(dplyr)
library(ggplot2)
data30g %>%
count(yy, sex, age) %>%
ggplot(aes(age, n)) +
geom_col(aes(fill = sex)) +
facet_grid(yy ~ .) +
theme_bw() +
scale_fill_manual(values = c("#000000", "#cccccc"))
不錯 - 我可以直接看到,例如,隨着時間的推移,30歲時的總數和女性數量都會增加,但也許有點小而且擁擠。
我們可以使用小平面包裹而不是網格來使條形更清晰,但代價是多年來的快速視覺比較。
data30g %>%
count(yy, sex, age) %>%
ggplot(aes(age, n)) +
geom_col(aes(fill = sex)) +
facet_wrap(~yy, ncol = 2) +
theme_bw() +
scale_fill_manual(values = c("#000000", "#cccccc"))
還有一個例子沒有用總計數或條形圖來解決你的問題 - 但我認為這可能是有意義的。 此代碼生成“熱圖”樣式的情節,這對於定量比較來說很差,但有時可以給出有趣特征的快速視覺印象。 例如,我認為這表明2014年的20歲女性總人數最多。
data30g %>%
count(yy, sex, age) %>%
ggplot(aes(factor(age), yy)) +
geom_tile(aes(fill = n)) +
facet_grid(sex ~ .) +
scale_fill_gradient2() +
scale_y_reverse(breaks = 2006:2015) +
labs(x = "age", y = "Year")
編輯:
根據評論中的進一步討論,這里有一種方法將年齡作為一個因素,使用性別欄,用總線覆蓋並按年份分開。
overallpierc %>%
count(yy, sex, age) %>%
ggplot() +
geom_col(aes(factor(age), n, fill = sex), position = "dodge") +
stat_summary(aes(factor(age), n), fun.y = "sum", geom = "line", group = 1) +
facet_grid(yy ~ .)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.