geom_bar(aes =)和position_dodge(width =)中的"width ="以什么单位呈现? [英] What units are the 'width = ' in geom_bar(aes = ) and position_dodge(width = ) rendered in?

查看:127
本文介绍了geom_bar(aes =)和position_dodge(width =)中的"width ="以什么单位呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在其中一个闪避的条上专门放置一些东西,我怎么能做到这一点?

I want to layer something specifically on one of the dodged bars, how would I be able to do that?

在下面的示例中,我想在条形图的cat3组中的A3上专门分层.我知道cat3的x = 3,但由于闪避宽度和小节宽度的相互依存关系,似乎很难瞄准该小节.我可以使用任何公式来计算x坐标吗?我在下面提供了示例代码.谢谢

In the below example, I want to layer something specifically on A3 in the cat3 group on the barplot. I know that x = 3 for cat3 but because of the interdependence of the widths for dodge and bar width it seems difficult to target that one bar. Is there any formula that i can use to calculate the x coordinate? I have provided an example code below. thanks

x1 <- c(15,75,28,60,80,100)
x2 <- c('cat1','cat2','cat3','cat1','cat2','cat3')
x3 <- c('A1','A2','A3','A4','A1','A2')
data=data.frame(cbind(x1,x2,x3),stringsAsFactors = F)
data$x1 <- as.numeric(data$x1)
data$x2 <- factor(data$x2)
data$x3 <- factor(data$x3)

ggplot(data,aes(y = x1, x = x2)) + geom_bar(stat = 'identity', 
                                 aes(fill = data$x3, width = 0.5), 
                                 position = position_dodge(width = 0.8))

推荐答案

position_dodge()中的width参数指定左栏的最左边缘和右栏的最右边缘之间的距离.闪避宽度为0.8时,x3类别的x = 3的起点与任一条边之间的距离为0.4(右侧为+0.4,左侧为-0.4),即0.4(即0.2)的一半将您带到条的中点(再次在+0.2处在左侧,-0.2处).不论钢筋宽度如何,都是如此.

The width argument in position_dodge() is specifying the distance between the leftmost edge of the left bar and the rightmost edge of the right bar. With a dodge width of 0.8, the distance between your start point of x = 3 for your x3 category and the edge of either bar is 0.4 (+0.4 on the right and -0.4 on the left) Half of 0.4 (i.e. 0.2) will bring you to midpoint of the bar (again +0.2 on the right and -0.2 on the left). This is true irrespective of the bar width.

在此示例中,我在cat3的右栏中绘制了一个H. y单位与y轴上的对齐.

Here's an example where I've drawn an H on the right bar in cat3. The y-units line up with those on the y-axis.

ggplot(data,aes(y = x1, x = x2)) + 
    geom_bar(stat = 'identity',
             aes(fill = x3, width = 0.5), 
             position = position_dodge(width = 0.8))+
    geom_text(aes(x = 3.2, y = 25, label = "H"), size = 10)

这篇关于geom_bar(aes =)和position_dodge(width =)中的"width ="以什么单位呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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