ggplot图中的天数

问题描述 投票:0回答:1

如何在X轴上得到1到365天?我特别需要将365天作为最后的日期。

ggplot(co21, aes(x = day2, y = cycle,  color = year, fill = year)) + geom_line() +
    labs(title = expression(paste(CO[2]," emission")),
         caption = "Source: NOAA Global Monitoring Laboratory",
         x = "Days",
         y = "Parts Per Millions")+
    theme_economist(base_size = 10))

head(co21)
  year month day  cycle  trend day2
1 2019     1   1 409.87 408.73    1
2 2019     1   2 409.89 408.73    2
3 2019     1   3 409.91 408.74    3
4 2019     1   4 409.93 408.75    4
5 2019     1   5 409.95 408.76    5
6 2019     1   6 409.98 408.76    6

这就是剧情。enter image description here

谢谢大家

r ggplot2 days
1个回答
0
投票

你可以使用 scale_x_continuous 函数,因为您的 x 轴是一个连续变量。在你的X轴是连续变量的情况下,你的 breaks 参数允许你指定其中之一。

  • NULL 为不中断
  • waiver() 计算的默认断点。
  • 一个位置的数字向量
  • 一个将极限作为输入并将中断作为输出的函数(例如,由 scales::extended_breaks())

在我们的例子中,我们将使用第三个选项,为tick指定一个位置的向量。您的代码将显示从1到最大数量的每一个增量的tick。day2 会因此。

ggplot(co21, aes(x = day2, y = cycle,  color = year, fill = year)) + 
    geom_line() +
    labs(title = expression(paste(CO[2]," emission")),
         caption = "Source: NOAA Global Monitoring Laboratory",
         x = "Days",
         y = "Parts Per Millions") +
    theme_economist(base_size = 10)) +
    scale_x_continuous(breaks=seq(1,max(co21$day2)))

0
投票

这里有一个方法。诀窍是指定 breaks. 请参见文件。

df <- tibble(days = seq(1,365),y = rnorm(365))

ggplot(df,aes(days,y)) + 
  geom_line() + 
  scale_x_continuous(limits = c(0,365),
                     breaks = c(seq(0,365,25),365))
© www.soinside.com 2019 - 2024. All rights reserved.