R ggplot2中连续轴上的字符值 [英] Character values on a continuous axis in R ggplot2

查看:210
本文介绍了R ggplot2中连续轴上的字符值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用ggplot2绘制连续数据时,有没有办法在轴上包含字符值?我有审查数据,如:

pre $ code> xy Freq
1 -3 16 3
2 -2 12 4
3 0 10 6
4 2 7 7
5 2 4 3

最后一行数据是右删失的。我正在用下面的代码来绘制它以产生下面的图:

  a1 = data.frame(x = c(-3 ,-2,0,2,2),y = c(16,12,10,7,4),Freq = c(3,4,6,7,3))
fit = ggplot(a1 ,aes(x,y))+ geom_text(aes(label = Freq),size = 5)+
theme_bw()+
scale_x_continuous(breaks = seq(min(a1 $ x)-1,
labels = seq(min(a1 $ x)-1,max(a1 $ x)+ 1,by = 1),
limits = c(min(a1 $ x)-1,max(a1 $ x)+1))+
scale_y_continuous(breaks = seq(min(a1 $ y),max(a1 $ y),by = 2 ))


在(2,4)处的3点是正确的审查。我希望他们在右边绘制一个单位,相应的x轴刻度标记'> = 2',而不是3个。任何想法,如果这是可能的?

解决方案

这是完全可能的。我破解了数据,所以 2,4 它是 3,4 。然后我修改了你的标签,只要它们的长度与休息时间相同即可。

  ggplot(a1 ,aes(x,y))+ geom_text(aes(label = Freq),size = 5)+ 
theme_bw()+
scale_x_continuous(breaks = seq(min(a1 $ x)-1, (a1 $ x),by = 1),
labels = c(seq(min(a1 $ x)-1,max(a1 $ x)-1,by = 1),> = 2 ),
limits = c(min(a1 $ x)-1,max(a1 $ x)))+
scale_y_continuous(breaks = seq(min(a1 $ y),max(a1 $ y),by = 2))


Is there a way to include character values on the axes when plotting continuous data with ggplot2? I have censored data such as:

   x  y Freq
1 -3 16    3
2 -2 12    4
3  0 10    6
4  2  7    7
5  2  4    3

The last row of data are right censored. I am plotting this with the code below to produce the following plot:

a1 = data.frame(x=c(-3,-2,0,2,2), y=c(16,12,10,7,4), Freq=c(3,4,6,7,3))
fit = ggplot(a1, aes(x,y)) + geom_text(aes(label=Freq), size=5)+
  theme_bw() +
  scale_x_continuous(breaks = seq(min(a1$x)-1,max(a1$x)+1,by=1),
                     labels = seq(min(a1$x)-1,max(a1$x)+1,by=1),
                     limits = c(min(a1$x)-1,max(a1$x)+1))+
  scale_y_continuous(breaks = seq(min(a1$y),max(a1$y),by=2))

The 3 points at (2,4) are right censored. I would like them to be plotted one unit to the right with the corresponding xaxis tick mark '>=2' instead of 3. Any ideas if this is possible?

解决方案

It is quite possible. I hacked the data so 2,4 it's 3,4. Then I modified your labels which can be whatever you want as long as they are the same length as the breaks.

ggplot(a1, aes(x,y)) + geom_text(aes(label=Freq), size=5)+
theme_bw() +
scale_x_continuous(breaks = seq(min(a1$x)-1,max(a1$x),by=1),
                   labels = c(seq(min(a1$x)-1,max(a1$x)-1,by=1), ">=2"),
                   limits = c(min(a1$x)-1,max(a1$x)))+
scale_y_continuous(breaks = seq(min(a1$y),max(a1$y),by=2))

这篇关于R ggplot2中连续轴上的字符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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