垂直折线图 - 在 R 中将折线图方向更改为自上而下
vertical line chart - change line plotting direction to top-down in R
我正在寻找一种按照自上而下的方式连接数据点以可视化排名的方式。因为 y 轴表示等级,x 轴表示属性。在正常设置下,线从左到右连接点。这导致点的连接顺序错误。
线下的数据应从 (6,1) 连接到 (4,2),然后连接 (5,3) 等。最佳排名比例需要反转,以便排名第一从顶.
data <- read.table(header=TRUE, text='
attribute rank
1 6
2 5
3 4
4 2
5 3
6 1
7 7
8 11
9 10
10 8
11 9
')
plot(data$attribute,data$rank,type="l")
有没有办法改变画线方向?我的第二个想法是旋转图形,或者您可能有更好的想法。
我试图实现的图表与此有点相似:
example vertical line chart
你可以用 ggplot 做到这一点:
library(ggplot2)
ggplot(data,aes(y=attribute,x=rank)) + geom_line() + coord_flip() +
scale_x_reverse()
它完全按照您建议的方式解决了问题。命令的第一部分 (ggplot(...) + geom_line()
) 创建一个 "ordinary" 线图。请注意,我已经切换了 x 和 y 坐标。下一个命令 (coord_flip()
) 翻转 x 轴和 y 轴,最后一个命令 (scale_x_reverse
) 改变 x 轴的顺序(绘制为 y 轴)使得 1在左上角。
只是为了向您展示您在问题中链接的示例可以使用 ggplot2
完成,我添加以下示例:
ibrary(reshape2)
data$attribute2 <- sample(data$attribute)
data$attribute3 <- sample(data$attribute)
plot.data <- melt(data,id="rank")
ggplot(plot.data,aes(y=value,x=rank,colour=variable)) + geom_line() +
geom_point() + coord_flip() + scale_x_reverse()
如果你打算用 R 作图,学习 ggplot2
真的很值得。您可以在 Cookbook for R.
上找到许多示例
我正在寻找一种按照自上而下的方式连接数据点以可视化排名的方式。因为 y 轴表示等级,x 轴表示属性。在正常设置下,线从左到右连接点。这导致点的连接顺序错误。
线下的数据应从 (6,1) 连接到 (4,2),然后连接 (5,3) 等。最佳排名比例需要反转,以便排名第一从顶.
data <- read.table(header=TRUE, text='
attribute rank
1 6
2 5
3 4
4 2
5 3
6 1
7 7
8 11
9 10
10 8
11 9
')
plot(data$attribute,data$rank,type="l")
有没有办法改变画线方向?我的第二个想法是旋转图形,或者您可能有更好的想法。
我试图实现的图表与此有点相似: example vertical line chart
你可以用 ggplot 做到这一点:
library(ggplot2)
ggplot(data,aes(y=attribute,x=rank)) + geom_line() + coord_flip() +
scale_x_reverse()
它完全按照您建议的方式解决了问题。命令的第一部分 (ggplot(...) + geom_line()
) 创建一个 "ordinary" 线图。请注意,我已经切换了 x 和 y 坐标。下一个命令 (coord_flip()
) 翻转 x 轴和 y 轴,最后一个命令 (scale_x_reverse
) 改变 x 轴的顺序(绘制为 y 轴)使得 1在左上角。
只是为了向您展示您在问题中链接的示例可以使用 ggplot2
完成,我添加以下示例:
ibrary(reshape2)
data$attribute2 <- sample(data$attribute)
data$attribute3 <- sample(data$attribute)
plot.data <- melt(data,id="rank")
ggplot(plot.data,aes(y=value,x=rank,colour=variable)) + geom_line() +
geom_point() + coord_flip() + scale_x_reverse()
如果你打算用 R 作图,学习 ggplot2
真的很值得。您可以在 Cookbook for R.