gpt4 book ai didi

r - 如何在 ggplot2 的 x 轴中使用月份名称

转载 作者:行者123 更新时间:2023-12-04 10:11:11 25 4
gpt4 key购买 nike

我尝试使用 ggplot2 在 x 轴上绘制月份,但月份名称会自动显示为带小数的数字。如何强制脚本绘制插入数字的月份名称?我使用了这个代码:

ggplot(df3, aes(x = month, y = PM)) + 
geom_line(aes(col = factor(travel))) +
xlab("Month") +
ylab(expression(paste("PM(",mu,"g/", m^3,")", sep="")))

数据见下:
df3 <- structure(list(month = c(9, 10, 9, 10, 1, 2, 3, 4, 5, 9, 10, 
1, 2, 3, 4, 5, 9, 10, 11, 12, 1, 2, 3, 4, 5, 11),
travel = c("Diesel bus",
"Diesel bus", "Diesel bus", "Diesel bus", "Diesel bus", "Diesel car",
"Diesel car", "Diesel car", "Diesel car", "Diesel car", "Bicycle",
"Bicycle", "Bicycle", "Bicycle", "Bicycle", "Gasoline car", "Gasoline car",
"Gasoline car", "Gasoline car", "Gasoline car", "Elcectric bus",
"Elcectric bus", "Elcectric bus", "Elcectric bus", "Elcectric bus",
"Elcectric bus"),
PM = c(22.6496512922918, 18.1829352554303,
28.776408085308, 30.1441935430254, 23.8938954711914, 21.8288997171693,
22.7177263732526, 29.8606175809457, 30.530998468399, 30.1288699182287,
28.4038889338888, 19.4761033463478, 18.9449487406838, 20.3568456145256,
16.5431814479828, 12.8668955993652, 21.6255497367427, 21.8725590587368,
14.7631275227865, 12.5790810203552, 15.1794028663635, 19.3508881492176,
15.895525373979, 15.3945024820964, 15.5982689292758, 12.1219868087769
)),
.Names = c("month", "travel", "PM"),
row.names = c(NA, -26L),
class = "data.frame")

最佳答案

您也可以使用 scale_x_discrete

ggplot(df3, aes(x=month, y=PM)) +
geom_line(aes(col = factor(travel)))+
xlab("Month")+ ylab (expression(paste("PM(",mu, "g/", m^3,")", sep=""))) +
scale_x_discrete(labels=month.abb)

关于r - 如何在 ggplot2 的 x 轴中使用月份名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com