我正在尝试使用 ggplot 在 R 中创建折线图

发布于 2025-01-10 23:33:19 字数 412 浏览 0 评论 0原文

我对 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 轴是百分比对于特定年龄来说是的。

我不太确定如何创建百分比

有什么建议吗?谢谢你!

I am new to R and programming in general, I have a data frame similar to this but with a lot more rows:

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)

I am trying to create a line graph using ggplot where the x-axis is the age and the y axis is the percentage of yes for a specific age.

I am not too sure how to create the percentage

any advice? thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄兵必败 2025-01-17 23:33:19

另一个解决方案堆叠条形图:

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)

绘制绘图:

    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))

结果: 在此处输入图像描述

Another solution stacked bar chart:

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)

Draw the 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))

Outcome: enter image description here

无语# 2025-01-17 23:33:19
data %>%
  group_by(age, yes_no) %>%
  mutate(k = n()) %>%
  ungroup(yes_no) %>%
  mutate(n = n(),
         p = 100*k/n) %>%
  unique() %>%
  ungroup() %>%
  complete(age, yes_no,
           fill = list(k = 0, n = 0, p = 0))  %>%
  ggplot(aes(x=age, y =p, group = yes_no, color = yes_no)) +
  geom_line() +
  ylim(c(0,100))

输入图片此处描述

data %>%
  group_by(age, yes_no) %>%
  mutate(k = n()) %>%
  ungroup(yes_no) %>%
  mutate(n = n(),
         p = 100*k/n) %>%
  unique() %>%
  ungroup() %>%
  complete(age, yes_no,
           fill = list(k = 0, n = 0, p = 0))  %>%
  ggplot(aes(x=age, y =p, group = yes_no, color = yes_no)) +
  geom_line() +
  ylim(c(0,100))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文