R 如何在x轴上创建包含平均值和比例变量(1-5)的堆叠条形图?

R 如何在x轴上创建包含平均值和比例变量(1-5)的堆叠条形图?,r,R,我得到了一个df,其中变量1-5是总计数的值 df<-data.frame( speed=c(2,3,3,2,2), race=c(5,5,4,5,5), cake=c(5,5,5,4,4), lama=c(2,1,1,1,2)) library(data.table) dcast(melt(df), variable~value) # variable 1 2 3 4 5 #1 speed 0 3 2 0 0 #2 race 0 0 0 1 4 #3

我得到了一个df,其中变量1-5是总计数的值

df<-data.frame(
  speed=c(2,3,3,2,2),
  race=c(5,5,4,5,5),
  cake=c(5,5,5,4,4),
  lama=c(2,1,1,1,2))

library(data.table)
dcast(melt(df), variable~value)

#  variable 1 2 3 4 5
#1    speed 0 3 2 0 0
#2     race 0 0 0 1 4
#3     cake 0 0 0 2 3
#4     lama 3 2 0 0 0 
我想用x轴上的平均值和比例变量1-5,按第一列速度、种族、蛋糕、喇嘛中的变量做叠加条形图

我试着从解决方案,但没有什么我正在寻找


考虑到我对问题的正确理解,我不得不尝试一些事情并做一些工作,以获得与您想要的非常接近的东西:

library(dplyr)
library(ggplot2)
library(tidyr)

df<-data.frame(
  speed=c(2,3,3,2,2),
  race=c(5,5,4,5,5),
  cake=c(5,5,5,4,4),
  lama=c(2,1,1,1,2))

 # get the data in right shape for ggplot2
 dfp <-  df %>% 
   # a column that identifies the rows uniquely is needed ("name of data row")
   dplyr::mutate(ID = as.factor(dplyr::row_number())) %>% 
   # the data has to shaped into "tidy" format (similar to excel pivot)
   tidyr::pivot_longer(-ID) %>% 
   # order by name and ID
   dplyr::arrange(name, ID) %>% 
   # group by name 
   dplyr::group_by(name) %>% 
   # calculate percentage and cumsum to be able to calculate label position (p2)
   dplyr::mutate(p = value/sum(value),
                 c= cumsum(p),
                 p2 = c - p/2,
                 # the groups or x-axis values have to be recoded to numeric type
                 name = recode(name, "cake" = 1, "lama" = 2, "race" = 3, "speed" = 4))

# calculate the mean value per group (or label) as you want them in the plot
sec_labels <- dfp %>%
  dplyr::summarise(m = mean(value)) %>%
  pull(m)

dfp %>% 
  # building base plot, telling to fill by the new name variable
  ggplot2::ggplot(aes(x = name, y = value, fill = ID)) +
  # make it a stacked bar chart by percentiles
  ggplot2::geom_bar(stat = "identity", position = "fill") +
  # recode the x axis labels and add a secondary x axis with the labels
  ggplot2::scale_x_continuous(breaks = 1:4,
                              labels = c("cake", "lama","race", "speed"),
                              sec.axis = sec_axis(~., 
                                                  breaks = 1:4,
                                                  labels = sec_labels)) +
  # flip the chart by to the side
  ggplot2::coord_flip() +
  # scale the y axis (now after flipping x axis) to percent
  ggplot2::scale_y_continuous(labels=scales::percent) +
  # add a layer with labels acording to p2
  ggplot2::geom_text(aes(label = value, 
                         y=p2)) +
  # put a name to the plot
  ggplot2::ggtitle("meaningfull plot name") +
  # put the labels on top 
  ggplot2::theme(legend.position = "top") 

这回答了你的问题吗@用户438383不,不需要。想解释一下原因吗?关于如何制作堆叠条形图,有很多很好的答案。另外,在发布之前通常会显示一些您尝试过的证据。@user438383 Idk,如何为X轴选择变量1-5,Idk如何向图表中添加平均值。长话短说,Idk如何创建与已发布图片类似的堆叠条形图。编辑您的问题以包含这些细节,否则人们会认为您需要标准条形图。