geom_vline在x轴上的垂直线与分类数据:ggplot2 [英] geom_vline vertical line on x-axis with categorical data: ggplot2

查看:1490
本文介绍了geom_vline在x轴上的垂直线与分类数据:ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类中的数据,如本文所述:
https://www.r-bloggers.com/from-continuous-to-categorical/
这使得查看哪些值是常见的更容易。创建这些类之后,我想创建一个带有不同类别频率的条形图,我使用以下示例代码进行操作:

  set.seed(1)
df.v< - data.frame(val = rnorm(1000,mean(4,sd = 2)))
df.v $ val.clss< - cut(df.v $ val,seq(min(df.v $ val),max(df.v $ val),1))
p1 < - ggplot(data = df.v)+
geom_bar(aes(val.clss))
plot(p1)

什么我无法弄清楚,如何在4条左右两条之间精确地添加一条垂直线,因此该线完全位于x轴值。
我找到了这篇文章,但这并没有帮助我:
如何获得垂直geom_vline到类日期的x轴?
任何帮助表示赞赏。也许我太新以至于无法将解决方案适应于我的数据框架,如果是的话,请原谅这个问题。

解决方案

如果您知道两条线的标签,您可以将它们的位置转换为数字(它们被映射到的因素),然后传递:

  myLoc < -  
(which(levels( df.v $ val.clss)==(2.99,3.99])+
which(levels(df.v $ val.clss)==(3.99,4.99]))/
2


p1 +
geom_vline(aes(xintercept = myLoc))

如果跳过组,则应该确保所有因子的等级都被绘制出来。当您有连续的数据时,最好不要删除中间级别。



pre $ p1 +
geom_vline(aes(xintercept = myLoc))+
scale_x_discrete(drop = FALSE)

或者,您可以将所有数据中的缺失级别放在一起(绘制之前并计算 myLoc ):

  df.v < - 小滴(df.v)

然后它将只包含那个被绘制的图片。



作为最后一个选项,您可以使用 geom_histogram 自动进行分箱,但将数据保留在原始比例上,

  ggplot(df.v 
,aes(val))+
geom_histogram(binwidth = 1)+
geom_vline(xintercept = 4)


I have data that is ordered in classes, as described in this article: https://www.r-bloggers.com/from-continuous-to-categorical/ This makes it easier to see which values are common. After creating those classes I want to create a barchart with the frequency of the different classes, which I do with the following exemplary code:

set.seed(1)
df.v <- data.frame(val = rnorm(1000, mean(4, sd=2)))
df.v$val.clss <- cut(df.v$val, seq(min(df.v$val), max(df.v$val), 1))
p1 <- ggplot(data = df.v)+
  geom_bar(aes(val.clss))
plot(p1)

What I can not figure out, is how to add a vertical line exactly between the two bars around 4, so the line is perfectly at the x-axis value. I have found this article, but this did not help me: How to get a vertical geom_vline to an x-axis of class date? Any help is appreciated. Maybe I am too new to adapt the solution to my data.frame, if so, please excuse the question.

解决方案

If you know the labels for the two bars you want the line to go between, you can convert their locations to numbers (the factor that they are mapped to), then pass that:

myLoc <- 
  (which(levels(df.v$val.clss) == "(2.99,3.99]") +
     which(levels(df.v$val.clss) == "(3.99,4.99]")) / 
  2


p1 +
  geom_vline(aes(xintercept = myLoc))

If it is skipping groups, you should probably make sure that all levels of the factor are plotted. When you have binned continuous data, it is best not to drop intermediate levels.

p1 +
  geom_vline(aes(xintercept = myLoc)) +
  scale_x_discrete(drop = FALSE)

Alternatively, you could drop the missing levels from the data all together (prior to plotting and to calculating myLoc):

df.v <- droplevels(df.v)

Then it will only include the that would be plotted.

As a final option, you could just use geom_histogram which does the binning automatically, but leaves the data on the original scale, which would make adding a line easier.

ggplot(df.v
       , aes(val)) +
  geom_histogram(binwidth = 1) +
  geom_vline(xintercept = 4)

这篇关于geom_vline在x轴上的垂直线与分类数据:ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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