[英]Im trying to create a line graph in R using ggplot
我是 R 和一般編程的新手,我有一個與此類似但行數更多的數據框:
yes_no <- c('Yes','No','No','Yes','Yes','No','No','No','Yes','Yes','No','Yes','No','Yes','No','Yes','No','Yes','No','Yes')
age <- c('1','1','2','3','4','5','1','2','2','3','1','5','5','5','1','4','4','2','5','3')
data<- data.frame(yes_no,age)
我正在嘗試使用 ggplot 創建折線圖,其中 x 軸是年齡,y 軸是特定年齡的“是”百分比。
我不太確定如何創建百分比
有什么建議嗎? 謝謝你!
另一個解決方案堆積條形圖:
Sample data:
yes_no<-c('Yes','No','No','Yes','Yes','No','No','No','Yes','Yes','No','Yes','No','Yes','No','Yes','No','Yes','No','Yes')
age <- c('1','1','2','3','4','5','1','2','2','3','1','5','5','5','1','4','4','2','5','3')
data<- data.frame(yes_no,age)
繪制 plot:
ggplot(data, aes(x = factor(age), fill = factor(yes_no))) +
geom_bar(position="fill", width = 0.7)+
geom_text(
aes(label=signif(..count.. / tapply(..count.., ..x.., sum)[as.character(..x..)], digits=3)),
stat="count",
position=position_fill(vjust=0.5)) +
labs(x="Age", y="Percentage", title="", fill="")+
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, face="bold", size=20, color="black")) +
theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black"))+
theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black"))+
theme(axis.text.x = element_text( hjust = 1, face="bold", size=14, color="black") )+
theme(axis.text.y = element_text( hjust = 1, face="bold", size=14, color="black") )+
theme(plot.title = element_text(hjust = 0.5))+
theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
legend.text = element_text(family="Times", color = "black", size = 14,face="bold"),
legend.position="bottom",
plot.title = element_text(hjust = 0.5))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.