我正在尝试为多面深度(Road=color)
中的多面图绘制的数据添加两条趋势线。 Geom_smooth
可以生成带有趋势线的第一个图(pred_new2)
,但是一旦我添加了facet_wrap
-生成了图,但是没有任何趋势线/平滑度,也没有任何错误。
pred_new$Site <- factor(pred_new$Site,
levels = c("A", "B", "C", "D", "E", "F", "G", "H", "I"))
pred_new2 <- ggplot(pred_new, aes(x = No_cars, y = Site, color = Road)) +
geom_point() +
geom_smooth(aes(x = No_cars, y = Site, color = Road), method = "lm")
pred_new3 <- pred_new2 +
geom_errorbarh(aes(xmin = No_cars - standerror, xmax = No_cars + standerror))
pred_new4 <- pred_new3 +
facet_wrap(~ Days, scales = "free_x") +
ylab("Site") +
xlab("No_cars") +
theme_classic()
pred_new4
任何帮助将不胜感激。
pred_new = structure(list(Site = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L),
.Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I"),
class = "factor"),
Days = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("Thursday", "Tuesday", "Wednesday"),
class = "factor"),
Road = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L),
.Label = c("east", "west"),
class = "factor"),
No_cars = c(15.266427, 8.323348, 8.368608, 9.747807, 7.976356, 8.5684, 6.604537,
3.812109, 6.719904, 4.799487, 4.996091, 4.796, 4.991479, 4.525789,
5.115136, 4.939559, 4.783792, 4.185007, 3.857553, 3.095228, 2.890727,
3.132784, 3.352974, 3.42561, 2.900284, 2.35416, 2.889976, 17.266427,
10.323348, 10.368608, 11.747807, 9.976356, 10.5684, 8.604537, 5.812109,
8.719904, 6.799487, 6.996091, 6.796, 6.991479, 6.525789, 7.115136,
6.939559, 6.783792, 6.185007, 5.857553, 5.095228, 4.890727, 5.132784,
5.352974, 5.42561, 4.900284, 4.35416, 4.889976),
standerror = c(1.7108483, 0.8175014, 0.6365042, 0.7171749, 0.9978123, 0.9881427,
0.9215597, 0.6365042, 1.6303975, 0.404129, 0.1934362, 0.1503158,
0.1694848, 0.2362161, 0.2337497, 0.2180687, 0.1604379, 0.3902528,
0.3276444, 0.1568268, 0.1218673, 0.1374084, 0.1915103, 0.1895107,
0.1767974, 0.1300738, 0.3163943, 1.7108483, 0.8175014, 0.6365042,
0.7171749, 0.9978123, 0.9881427, 0.9215597, 0.6365042, 1.6303975,
0.404129, 0.1934362, 0.1503158, 0.1694848, 0.2362161, 0.2337497,
0.2180687, 0.1604379, 0.3902528, 0.3276444, 0.1568268, 0.1218673,
0.1374084, 0.1915103, 0.1895107, 0.1767974, 0.1300738, 0.3163943)),
row.names = c(NA, -54L), class = "data.frame")
答案 0 :(得分:1)
我想您交换了轴。要包含趋势,您需要Site
的数值,因此我在as.numeric(Site)
中包含了ggplot
。
library(ggplot2)
ggplot(pred_new, aes(x = as.numeric(Site), y = No_cars, color = Road)) +
geom_point() +
geom_smooth(method = "lm") +
geom_errorbar(aes(ymin = No_cars - standerror, ymax = No_cars + standerror)) +
xlab("Site") +
# this will recode your x-axis
scale_x_continuous(breaks = 1:9, labels = LETTERS[1:9]) +
facet_wrap(~ Days, scales = "free_x") +
theme_classic() +
# you can swap x and y axes with coord_flip()
coord_flip()
这是所需的输出吗?
如果您确实希望在y轴上使用Site
,则只需翻转x和y轴即可:
ggplot(pred_new, aes(y = as.numeric(Site), x = No_cars, color = Road)) +
geom_point() +
geom_smooth(method = "lm") +
geom_errorbarh(aes(xmin = No_cars - standerror, xmax = No_cars + standerror)) +
ylab("Site") +
# this will recode your y-axis
scale_y_continuous(breaks = 1:9, labels = LETTERS[1:9]) +
facet_wrap(~ Days, scales = "free_x") +
theme_classic()