如何更改X轴编号? [英] How do you change the X axis number?
问题描述
p = ggplot() +
geom_line(data = Month_time, aes(x = Month, y = CarrierDelay), color = "red") +
geom_line(data = Month_time, aes(x = Month, y = WeatherDelay), color = "purple") +
geom_line(data = Month_time, aes(x = Month, y = NASDelay), color = "yellow") +
geom_line(data = Month_time, aes(x = Month, y = SecurityDelay), color = "green") +
geom_line(data = Month_time, aes(x = Month, y = LateAircraftDelay), color = "blue") +
xlab('Month') +
ylab('Delay Types [min]')
print(p)
Q如何将本月的X轴更改为仅1,2,3,4 ...? 问另外,如何为每个图形添加标签?
Q How do you change the X-axis the Month to be just 1, 2, 3, 4 ...? Q Also how do you add the label for each graph?
推荐答案
在这里,示例代码应重新组织您的数据以准备由ggplot2进行绘制:
Here, a sample of code that should re-organize your data to be ready to be plot by ggplot2:
library(tidyr)
library(dplyr)
library(ggplot2)
Month_subset = Month_time %>% select(., Month, CarrierDelay, WeatherDelay, NASDelay, SecurityDelay, LateAircraftDelay) %>%
pivot_longer(-Month,names_to = "Conditions", values_to = "Value")
ggplot(Month_subset, aes(x = Month, y = Value, color = Conditions)) +
geom_line() +
ylab("Delay Types [min]") +
scale_x_continuous(breaks = 1:12)
基本上,使用dplyr
和tidyr
,然后转到感兴趣的select
列,然后使用pivot_longer
以长格式重新组织数据.最后,我们将使用ggplot2
一次绘制所有内容.这样,您的每个条件都将以不同的颜色绘制,并添加说明每个标签的图例.
Basically, using dplyr
and tidyr
, we are going to select
columns of interest then, re-organise the data in a long format using pivot_longer
. And finally, we are going to plot everything in once using ggplot2
. Like that, each of your conditions will be plot with a different color and a legend will be added mentioning each labels.
警告,此代码基于我对您正在尝试执行的操作的了解,但由于您未提供
Warning this code is based on what I understand of what you are trying to do, but I can't verify it since you are not provding a reproducible example of your dataset. If you are adding one in your question, I will be happy to test my code and update it based on your real data
这篇关于如何更改X轴编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!