geom_bar中条的宽度相同(position =“dodge”) [英] The same width of the bars in geom_bar(position = "dodge")

查看:1981
本文介绍了geom_bar中条的宽度相同(position =“dodge”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制宽度相同的条形图。这是我最小的示例代码:

  data<  -  data.frame(A = letters [1:17],
=样本(1:500,17),
C = c(rep(1,5),rep(2,6),rep(c(3,4,5),each = 2)) )

ggplot(data,
aes(x = C,y = B,label = A,
fill = A))+
geom_bar(stat =身份,position =dodge)+
geom_text(position = position_dodge(width = 0.9),angle = 90)

结果如上图所示:



我想要的是在第一张图中绘制情节,但是栏的宽度与列 C 中每个级别的观察数无关。

geom_bar(width)更改bars组的宽度,但仍然第五组中的酒吧比第一组酒吧宽,所以它不是我问题的答案。

可以像这样



或使用交互项:

  p < -  ggplot(data,aes(x =互动(C,A),y = B,fill = A))
p + geom_bar(stat =identity,position =dodge)



最后将交互转换为数字,您可以

  p < -  ggplot(data,aes(x = as.numeric) (交互(C,A)),y = B,fill = A,label = A))
p < - p + geom_bar(stat =identity,position =dodge)+
geom_text(position = position_dodge(width = 0.9),angle = 90)
p + scale_x_continuous(breaks = aggregate(with(data,as.numeric(interaction(C,A))),list(data $ C),表示)[,2],
labels = c(1:5))

<一个href =https://i.stack.imgur.com/3kr HW.pngrel =nofollow noreferrer>



编辑:

另一种方法是使用方面。使用space =free_x可以设置宽度与x比例的长度成比例。

  library(tidyverse)
data%>%
ggplot(aes(x = A,y = B,fill = A))+
geom_bar(stat =identity,position =dodge)+
facet_grid(〜C,scales =free_x,space =free_x)


I would like to draw plot with the same width of the bars. Here's my minimal example code:

data <- data.frame(A = letters[1:17],
                   B = sample(1:500, 17),
                   C = c(rep(1, 5), rep(2, 6), rep(c(3,4,5), each = 2)))

ggplot(data,
       aes(x = C,  y = B, label = A,
           fill = A)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(position = position_dodge(width = 0.9), angle = 90)

The result is shown in the picture above:

The width of the bars is dependent on numbers of observation in group given in variable C. I want to have each bar to have the same width.

The facet_grid(~C) works (bars are the same width) it's not what I mean:

ggplot(data,
       aes(x = C,  y = B, label = A,
           fill = A)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(position = position_dodge(width = 0.9), angle = 90) +
  facet_grid(~C)

What I want is to have plot like in the first picture but with bars's width independent on number of observation in each level from column C. How can I do it?

[EDIT] geom_bar(width) changes width of the bars'group but still bars in fifth group are wider than in the first group, so it's not the answer to my question.

解决方案

As already commented you can do it like in this answer Make factors and rbind unseen variables:

data$A <- as.factor(data$A)
data$C <- as.factor(data$C)
dat.all <- rbind(data[,c(1,3,2)], cbind(expand.grid(A=levels(data$A),C=levels(data$C)), B=NA))
p <- ggplot(dat.all, aes(x = C,  y = B, fill = A)) 
p +  geom_bar(stat = "identity", position = "dodge")

Or work with an interaction term:

p <- ggplot(data, aes(x = interaction(C, A),  y = B, fill = A)) 
p +  geom_bar(stat = "identity", position = "dodge")

And finally transforming the interaction to numeric you can setup the x-axis according to your desired output:

p <- ggplot(data, aes(x = as.numeric(interaction(C, A)),  y = B, fill = A,label = A)) 
p <- p +  geom_bar(stat = "identity", position = "dodge")+
      geom_text(position = position_dodge(width = 0.9), angle = 90 )
p + scale_x_continuous(breaks = aggregate(with(data, as.numeric(interaction(C, A))), list(data$C), mean)[, 2],
               labels = c(1:5))

Edit:

Another approach would be to use facets. Using space = "free_x" allows to set the width proportional to the length of the x scale.

library(tidyverse)
data %>% 
  ggplot(aes(x = A,  y = B, fill = A))  +  
   geom_bar(stat = "identity", position = "dodge") +
   facet_grid(~C, scales = "free_x", space = "free_x")

这篇关于geom_bar中条的宽度相同(position =“dodge”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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