R ggplot2中连续轴上的字符值

Gle*_*len 5 r axes ggplot2

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

   x  y Freq
1 -3 16    3
2 -2 12    4
3  0 10    6
4  2  7    7
5  2  4    3
Run Code Online (Sandbox Code Playgroud)

最后一行数据经过右删失。我正在使用下面的代码绘制此图以生成以下图:

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))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

(2,4) 处的 3 个点是右删失的。我希望将它们绘制到右侧一个单位,并使用相应的 xaxis 刻度标记“>=2”而不是 3。如果可能,有什么想法吗?

Luc*_*zer 5

这是很有可能。我破解了数据,所以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,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))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明