在不同的图上相互绘制不同的变量

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

我有3个不同的变量(A,B,C)要在彼此顶部的3个图表上绘制(因为它们具有不同的轴)。我的输出在图之间有很多空间,我想减少该空间,并且在底部只有X轴,并且所有图的y限制都更大。

我也阅读过facet_wrap也许是绘制多个图形的更好方法?您能给我什么最好的建议吗?谢谢

我的数据:

Location = c(1,2,3,4,5,6,7)
A = c(1.16, 0.96, 0.67,0.78, 0.55, 0.3,0.26)
B = c(6.51, 4.98, 2.85, 3.19, 3.60, 10.82, 8.60)
C = c(75.45, 86.66, 103.36, NA, 107.53, NA, 128.49)

df = data.frame(Location, A, B, C)

我的代码:

par(mfrow=c(3,1))

plot(A, type = "l", col = "red", ylab = "A", main="Title", xlab = NULL)
plot(B, type = "l", col = "Blue", ylab = "B", xlab = NULL)
plot(C, type = "p", pch= 19, col = "Blue", ylab = "C", xlab = Location)

enter image description here

r plot facet-wrap
2个回答
2
投票

这里您在末尾有x标签“位置”

par(mfrow=c(3,1))

plot(A, type = "l", col = "red", ylab = "A", main="Title", xlab = "")
plot(B, type = "l", col = "Blue", ylab = "B", xlab = "")
plot(C, type = "p", pch= 19, col = "Blue", ylab = "C", xlab = "Location")

2
投票

[这里是facet_wrap的选项。我对您的数据进行了稍微的重组。

df <- data.frame(Location = rep(Location, 3), y = c(A,B,C), letter = rep(c("A","B","C"), each = 7))

library(ggplot2)

ggplot(df, aes(x = Location, y = y, color = letter)) +
  geom_line() +
  geom_point() +
  scale_x_continuous(breaks = c(1:7), labels = c(1:7)) +
  facet_wrap(~letter, nrow = 3, scales = "free_y") +
  scale_color_manual(values = c("red", "blue", "blue")) + 
  theme(legend.position = "none")
© www.soinside.com 2019 - 2024. All rights reserved.