【问题标题】:ggplot2 not displaying values of the Y axisggplot2不显示Y轴的值
【发布时间】:2018-08-29 12:21:32
【问题描述】:

我尝试获取显示 Y 轴的值,但似乎无法解决问题,这是我的代码和除数据之外的绘图结果:

library(ggplot2)

traffic.data <- structure(list(years = 2008:2017, units = structure(c(1L, 2L, 4L, 5L, 3L, 6L, 7L, 8L, 9L, 10L), .Label = c("12,866,461", "13,350,011", "15,106,666", "15,361,841", "15,669,918", "16,498,204", "17,296,885", "17,611,762", "18,239,288", "20,359,883"), class = "factor")), .Names = c("years", "units"), class = "data.frame", row.names = c(NA, -10L))

traffic.data
       years     units
#  1   2008 12,866,461
#  2   2009 13,350,011
#  3   2010 15,361,841
#  4   2011 15,669,918
#  5   2012 15,106,666
#  6   2013 16,498,204
#  7   2014 17,296,885
#  8   2015 17,611,762
#  9   2016 18,239,288
# 10  2017  20,359,883

ggplot(traffic.data, aes(x = years, y = as.numeric(units))) + 
   geom_point() +
   geom_line() +
   scale_x_continuous(breaks = seq(2008, 2017, 1)) + 
   scale_y_continuous(breaks = seq(10000000,30000000,1)) +
   labs(x = "years", y = "total traffic of passengers", 
        title = "evolution of traffic during the past 10 years")

当我运行此代码时,我得到以下折线图缺少 Y 轴的值:

【问题讨论】:

  • 如果您能提供可重现的数据,那就太好了。
  • 您所说的可重现数据是什么意思? ps:还是 R 上的新手
  • 使用as.numeric(gsub(",", "", as.character(units), fixed = TRUE))
  • 关于“您所说的可重现数据是什么意思?”,请研究R tag wiki
  • @Dan 这是我得到的:结构(列表(年 = 2008:2017,单位 = 结构(c(1L,2L,4L,5L,3L,6L,7L,8L,9L, 10L),.Label = C(“12,866,461”,“13,350,011”,“15,106,666”,“15,361,841”,“15,669,918”,“16,498,204”,“17,296,885”,“17,611,762”,“18,239,288”,“20,359,883”,“20,359,883”),“20,359,883”,“20,359,883”),class= "因子")), .Names = c("years", "units"), class= "data.frame", row.names = c(NA, -10L))

标签: r ggplot2 plot linegraph


【解决方案1】:

问题一:

scale_y_continuous(breaks = seq(10000000,30000000,1))

所以,您正尝试将 10M 增加到 30M 1。

问题 2:

  +   labs(x = "years", y = "total traffic of passengers", 
    +        title = "evolution of traffic during the past 10 years")

在这一行中,不需要您的第二个 +,这会产生问题。


这些数据对我很有效

dt<-data.frame(years=c(2008:2018),
               units=sample(c(15000000:20000000),11,replace = T))
dt

还有这段代码:

       ggplot(dt, aes(x=years, y=units))+
   geom_point(data=NULL)+
   geom_line(data=NULL) + 
   scale_y_continuous(breaks = seq(10000000,30000000,1000000)) + 
   labs(x = "years", y = "total traffic of passengers",
        title = "evolution of traffic during the past 10 years")

结果如下:

【讨论】:

  • 我不想要十进制值,这就是原因
  • @LazyBrain 很好,但是数据集适合你吗?
  • 我怀疑数据集是导致问题的原因
  • @LazyBrain 非常欢迎您。这是我和 AntoniosK 之间的比赛
【解决方案2】:

数据集

# example dataset
df = structure(list(years = 2008:2017, 
                    units = structure(c(1L, 2L, 4L, 5L, 3L, 6L, 7L, 8L, 9L, 10L), .Label = c("12,866,461", "13,350,011", "15,106,666", "15,361,841", "15,669,918", "16,498,204", "17,296,885", "17,611,762", "18,239,288", "20,359,883"), class = "factor")), 
              .Names = c("years", "units"), class = "data.frame", row.names = c(NA, -10L))

问题

您的units 列是factor,将其更新为数字很棘手。检查这个:

as.numeric(df$units)
# [1]  1  2  4  5  3  6  7  8  9 10

这些是您在 y 轴上绘制的值。这就是为什么无论实际数字是多少,您的点之间的距离似乎都是相同的,这就是为什么这些值也没有显示在 y 轴上的原因,因为您指定了scale_y_continuous(breaks = seq(10000000,30000000,1))(即从 3mil 开始)。

解决方案

# update column
df$units = as.numeric(gsub(",", "", as.character(df$units), fixed = TRUE))

library(ggplot2)

ggplot(df, aes(x=years, y=units)) +
  geom_point() +
  geom_line() + 
  scale_x_continuous(breaks = seq(2008,2017,1))+ 
  scale_y_continuous(breaks = seq(10000000,30000000,1000000)) +
  labs(x = "years", y = "total traffic of passengers", title = "evolution of traffic during the past 10 years")

请注意,我已将 y 轴值之间的步长/距离更改为 1mil。步长 = 1 会使过程变慢并且情节难以阅读。

您可以在绘图前使用options(scipen = 999),以避免使用科学记数法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多