如何使用ggplot2在R中创建此图表?

如何使用ggplot2在R中创建此图表?,r,ggplot2,charts,R,Ggplot2,Charts,我在RStudio中使用R,我有以下数据帧 df1 <- data.frame( comp = c("A", "B", "C", "D", "E", "F"), Q2_2018 = c(27, 10, 6, 4, 3, 2), Q2_2019 = c(31, 12, 8, 6, 5, 4)) 可以用ggplot2完成吗 大多数情况下: library(tidyverse) df1 %>% gather(year, val, -comp) %>%

我在RStudio中使用R,我有以下数据帧

df1 <- data.frame(  
  comp = c("A", "B", "C", "D", "E", "F"),  
  Q2_2018 = c(27, 10, 6, 4, 3, 2),  
  Q2_2019 = c(31, 12, 8, 6, 5, 4))
可以用ggplot2完成吗

大多数情况下:

library(tidyverse)
df1 %>%
  gather(year, val, -comp) %>%
  group_by(comp) %>% 
    mutate(change = val / lag(val) - 1) %>%
    mutate(change_lab = if_else(!is.na(change),
                                scales::percent(change, 
                                        accuracy = 1, 
                                        prefix = if_else(change > 0, "+", "-")),
                                NA_character_)) %>% 
  ungroup() %>%

  ggplot(aes(comp, val, fill = year, label = val)) +
  geom_col(position = position_dodge()) +
  geom_text(position = position_dodge(width = 1), vjust = -0.5) +
  geom_point(aes(comp, val + 5, size = change), color = "lightgreen") +
  geom_text(aes(comp, val+5, label = change_lab)) +
  scale_size_area(max_size = 30) +
  guides(size= F) +
  theme_classic()

到目前为止,你尝试了什么?至少在你的问题中加入这个:libraryggplot2;图书馆2;图书馆;meltdf1,id.vars=comp%>%ggplotaesx=comp,y=value,fill=variable+geom\u barstat=identity,position=dodge@M-谢谢。我大部分时间都被显示%变化的圆圈所困扰。@user3115933我说了,至少这样你的问题就不会结束了。查看下一条评论,了解您还可以为问题添加哪些内容。有了这一点,当我问我如何在顶部添加圆圈而不是标签时,我肯定你会得到上升票我不是下降票,但我理解他们。干杯。图书馆GGPLOT2;图书馆2;图书馆DPLYR;df1%>%mutatemdiff=factorx=as.characterround100*Q2_2019-Q2_2018/Q2_2018,levels=as.characterround100*Q2_2019-Q2_2018/Q2_2018%>%melt.,id.vars=ccomp,mdiff%>%ggplotaesx=comp,y=value+geom_baraesfill=variable,stat=identity,position=dodge+facet\u grid=mdiff,scales=free_x+themepanel.spacing.x=unit0,线条,panel.spacing.y=unit0,线条在变宽到变长之前会发生变化,以避免滞后和其他并发症。干杯
library(tidyverse)
df1 %>%
  gather(year, val, -comp) %>%
  group_by(comp) %>% 
    mutate(change = val / lag(val) - 1) %>%
    mutate(change_lab = if_else(!is.na(change),
                                scales::percent(change, 
                                        accuracy = 1, 
                                        prefix = if_else(change > 0, "+", "-")),
                                NA_character_)) %>% 
  ungroup() %>%

  ggplot(aes(comp, val, fill = year, label = val)) +
  geom_col(position = position_dodge()) +
  geom_text(position = position_dodge(width = 1), vjust = -0.5) +
  geom_point(aes(comp, val + 5, size = change), color = "lightgreen") +
  geom_text(aes(comp, val+5, label = change_lab)) +
  scale_size_area(max_size = 30) +
  guides(size= F) +
  theme_classic()