向R barplot添加垂直分隔符和标签 [英] Add vertical separator and labels to R barplot

查看:544
本文介绍了向R barplot添加垂直分隔符和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数据框,从中制作条形图。然后,我试着用垂直线添加组分隔符,并通过创建一个新的x轴来在这些行之间放置标签:

  Group = c(1_1,1_2,1_3,2_1,2_2,3_1,3_2,3_3,3_4)数字(c( -  1.23,2.34,0.56,1.87,-2.40,5.54,-0.98,-2.31,6))
data = data.frame(Group,Value)
data
组值
1_1 -1.23
1_2 2.34
1_3 0.56
2_1 1.87
2_2 -2.40
3_1 5.54
3_2 -0.98
3_3 -2.31
3_4 6.0

barplot(data $ Value,xaxs =i,xaxt = n)

我试图将垂直线添加为组分隔符:



。在1_3和2_1之间



。在2_2和3_1之间



和地点标签:

。原点和第一个分隔符之间的1



。第一个和第二个分隔符之间的2



。第二个分隔符和轴端之间的3



例如,在绘制散点图时,它可以很好地处理连续轴,但不适用于分类轴。
有什么方法可以将组类别转换为数字,具体取决于它们在图上的位置?

解决方案

我想这就是你所描述的:

$ $ $ $ $ $ $ $ barplot(data $ Value,xaxs =i,xaxt =n )
abline(v = 3.7)
abline(v = 6.1)
text(3.7 / 2,5.9,label ='1')
text(4.85,5.9,label,label ='2')
text(8.5,5.9,label ='3')


I have the following data frame from which I make a bar plot. Then, I am trying to add Group separators with vertical lines and to place labels in between these lines by creating a new x-axis:

Group = c("1_1", "1_2", "1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4")
Value = as.numeric(c("-1.23", "2.34", "0.56", "1.87", "-2.40", "5.54", "-0.98", "-2.31", "6"))
data = data.frame(Group, Value)
data
Group   Value
1_1   -1.23
1_2   2.34
1_3   0.56
2_1   1.87
2_2   -2.40
3_1   5.54
3_2   -0.98
3_3   -2.31
3_4   6.0

barplot(data$Value, xaxs="i",xaxt="n")

I am trying to add vertical lines as Group separators:

. between "1_3" and "2_1"

. between "2_2" and "3_1"

And place labels:

. "1" between origin and first separator

. "2" between first and second separator

. "3" between second separator and end of axis

It works fine with a continuous axis when making a scatter plot for instance, but it does not work with a categorical axis. Is there any way to sort of converting the Group categories to number depending on their position on the plot?

解决方案

I think this is what you are describing:

barplot(data$Value, xaxs="i",xaxt="n")
abline(v = 3.7)
abline(v = 6.1)
text(3.7/2, 5.9, label = '1')
text(4.85, 5.9, label = '2')
text(8.5, 5.9, label = '3')

这篇关于向R barplot添加垂直分隔符和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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