更改ggplot X轴编号

时间:2019-04-10 15:36:17

标签: r ggplot2

我有一个这样的数据框

Data2 <- structure(list(year = c(2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 
2014L, 2015L, 2016L, 2017L, 2018L, 2019L, 2008L, 2009L, 2010L, 
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2018L, 2019L, 
2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 
2017L, 2018L, 2019L), variable = c("Yahoo", "Yahoo", "Yahoo", 
"Yahoo", "Yahoo", "Yahoo", "Yahoo", "Yahoo", "Yahoo", "Yahoo", 
"Yahoo", "Yahoo", "Amazon", "Amazon", "Amazon", "Amazon", "Amazon", 
"Amazon", "Amazon", "Amazon", "Amazon", "Amazon", "Amazon", "Amazon", 
"Google", "Google", "Google", "Google", "Google", "Google", "Google", 
"Google", "Google", "Google", "Google", "Google"), value = c(1L, 
0L, 0L, 0L, 1L, 1L, 3L, 4L, 3L, 10L, 9L, 0L, 0L, 1L, 0L, 0L, 
3L, 2L, 2L, 3L, 8L, 9L, 12L, 3L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 
0L, 1L, 2L, 4L, 0L)), class = "data.frame", row.names = c(NA, 
-36L))

,我想用这个来绘制它:

library(ggplot2)
ggplot(Data2, aes(year, variable, size = value, color = variable)) +
    geom_point()

enter image description here

然而,该图的问题在于x轴例如没有2007,但它有2007.5,气泡不在对称位置

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您可以手动将休息时间设置为整年:

ggplot(Data2, aes(year, variable, size = value, color = variable)) +
  geom_point() + 
  scale_x_continuous(breaks = seq(min(Data2$year),max(Data2$year),by=1))

enter image description here