用堆栈和闪避绘制条形图,并使闪避的条形彼此接触 [英] Making a bar plot with stack and dodge, and keep the dodged bars touching one another

查看:49
本文介绍了用堆栈和闪避绘制条形图,并使闪避的条形彼此接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下:

 库(ggplot2)图书馆(tidyr)图书馆(dplyr)DT<-structure(list(value = structure(c(2,3,3,4,4,4,5,6,6,7,7,8,8,9,9,1,1,2,3,4,4,5,5,6,6,7,7,8,8,9,9),标签= NA_character_,类别= c(已标记",数字"))),惩罚=结构(c(0,0,1,0,1,0,0,1,0,1,0,1,0、1、0、1、1、0、0、1、0、1、0、1、0、1、0、1、0、1),标签=",类= c(标记为,数字")),计数= c(1L,1L,2L,3L,3L,5L,11L,2L,30L,10L,48L,13L,62L,16L,1L,1L,1L,2L,7L,4L,10L,4L,19L,6L,33L,7L,39L,10L,50L,13L),类型= c("Truth",真实",真实",真实",真实",真实",真实",真实",真实",真实",真实",真实",真实",真实",税收",税收",税收",税收",税收",税收",税收",税收",税收",税收","Tax","Tax","Tax","Tax","Tax","Tax"),x_label = c("2_Truth"),"3_Truth","3_Truth","4_Truth","4_Truth","5_Truth","6_Truth","6_Truth","7_Truth","7_Truth","8_Truth","8_Truth","9_Truth","9_Truth","1_Tax","1_Tax","2_Tax","3_Tax","4_Tax","4_Tax","5_Tax","5_Tax","6_Tax","6_Tax","7_Tax","7_Tax","8_Tax","8_Tax","9_Tax","9_Tax")),row.names = c(NA,-30L),class = c("data.table","data.frame"))价值罚款计数类型x_label1:2 0 1真相2_真相2:3 0 1真相3_真相3:3 1 2真相3_真相...28:8 1 10税8_税29:9 0 50税9_税30:9 1 13税9_税 

我试图用堆积和躲避"进行条形图绘制.正如肯特·约翰逊(Kent Johnson)在

但是,我希望图表更像下面的图表(这是我开始的方式).至少具有相同值的条在一起但颜色不同的地方.如果可能的话,还可以使用密度函数,图例和x轴值.

有什么办法吗?

贿赂建议

解决方案

您可以尝试

  DT%>%mutate(value = as.character(value))%&%完整(交叉(值,类型,罚分),填充=列表(计数= NA))%&%ggplot(aes(x =值,y =计数,填充=类型))+geom_col(data =.%>%filter(penalty == 0),position = position_dodge(width = 0.9),alpha = 0.2)+geom_col(data =.%>%filter(penalty == 1),position = position_dodge(width = 0.9),alpha = 1)+geom_tile(aes(y = NA_integer_,alpha = factor(penalty))) 

I have data as follows:

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

DT <- structure(list(value = structure(c(2, 3, 3, 4, 4, 5, 6, 6, 7, 
7, 8, 8, 9, 9, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9
), label = NA_character_, class = c("labelled", "numeric")), 
    penalty = structure(c(0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 
    0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), label = "", class = c("labelled", 
    "numeric")), count = c(1L, 1L, 2L, 3L, 3L, 5L, 11L, 2L, 30L, 
    10L, 48L, 13L, 62L, 16L, 1L, 1L, 1L, 2L, 7L, 4L, 10L, 4L, 
    19L, 6L, 33L, 7L, 39L, 10L, 50L, 13L), type = c("Truth", 
    "Truth", "Truth", "Truth", "Truth", "Truth", "Truth", "Truth", 
    "Truth", "Truth", "Truth", "Truth", "Truth", "Truth", "Tax", 
    "Tax", "Tax", "Tax", "Tax", "Tax", "Tax", "Tax", "Tax", "Tax", 
    "Tax", "Tax", "Tax", "Tax", "Tax", "Tax"), x_label = c("2_Truth", 
    "3_Truth", "3_Truth", "4_Truth", "4_Truth", "5_Truth", "6_Truth", 
    "6_Truth", "7_Truth", "7_Truth", "8_Truth", "8_Truth", "9_Truth", 
    "9_Truth", "1_Tax", "1_Tax", "2_Tax", "3_Tax", "4_Tax", "4_Tax", 
    "5_Tax", "5_Tax", "6_Tax", "6_Tax", "7_Tax", "7_Tax", "8_Tax", 
    "8_Tax", "9_Tax", "9_Tax")), row.names = c(NA, -30L), class = c("data.table", 
"data.frame"))

   value penalty count  type x_label
 1:     2       0     1 Truth 2_Truth
 2:     3       0     1 Truth 3_Truth
 3:     3       1     2 Truth 3_Truth
...
28:     8       1    10   Tax   8_Tax
29:     9       0    50   Tax   9_Tax
30:     9       1    13   Tax   9_Tax

I tried to do a "bar plot with stack and dodge" as suggested in this link by Kent Johnson follows:

ggplot(DT, aes(x=x_label, y=count, fill=penalty)) +
  geom_bar(stat='identity') + labs(x='Value / Treatment') + 
  theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))

However, I would like the chart to be more like the chart below (which is how I started out). Where at least the bars with the same value are together but have a different colour. If possible, also with the density function, the legend and the x-axis values.

Is there any way to do this?

EDIT: Suggestion of brianavery

解决方案

you can try

DT %>%
  mutate(value =as.character(value)) %>% 
  complete(crossing(value,type, penalty), fill = list(count = NA)) %>% 
  ggplot(aes(x= value, y=count, fill =  type)) +
  geom_col(data = . %>% filter(penalty==0), position = position_dodge(width = 0.9), alpha = 0.2) + 
  geom_col(data = . %>% filter(penalty==1), position = position_dodge(width = 0.9), alpha = 1)  +
  geom_tile(aes(y=NA_integer_, alpha = factor(penalty)))

这篇关于用堆栈和闪避绘制条形图,并使闪避的条形彼此接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆