gpt4 book ai didi

r - ggplot : line plot for discrete x-axis

转载 作者:行者123 更新时间:2023-12-01 09:15:38 24 4
gpt4 key购买 nike

我有下表,但经过多次尝试无法绘制数据,以便 x 轴刻度线与 year 对齐。 .我找到了箱线图的解决方案,但不是 geom_line()
我怎样才能制作一年的离散标签?

以下解决方案不起作用

g + scale_x_discrete(limits=c("2013","2014","2015"))
g + scale_x_discrete(labels=c("2013","2014","2015"))

distance_of_moves
距离移动年
1 2.914961 2013
2 2.437516 2014
3 2.542500 2015

ggplot(data = distance_of_moves, aes(x = year, y = `distancemoved`, group = 1)) +
geom_line(color = "red", linetype = "dashed", size = 1.5) +
geom_point(color = "red", size = 4, shape = 21, fill = "white") +
ylab("平均移动距离") +
xlab("年")

enter image description here

最佳答案

可重现的例子:

data <- data.frame(dist=c(2.914, 2.437, 2.542), year=c(2013, 2014, 2015))
# this ensures that stuff will be ordered appropriately
data$year <- ordered(data$year, levels=c(2013, 2014, 2015))
ggplot(data, aes(x=factor(year), y=dist, group=1)) +
geom_line() +
geom_point()

enter image description here

将年份指定为有序因子将确保 x 轴正确排序,而不管级别出现的顺序如何(而在绘图美学中仅使用“因子(年份)”可能会导致问题)。

关于r - ggplot : line plot for discrete x-axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35209157/

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