使用xyplot和R绘制月度数据
我想用x轴上的月份来绘制这个数据框使用xyplot和R绘制月度数据,r,hmisc,R,Hmisc,我想用x轴上的月份来绘制这个数据框 month value1 value2 value3 value4 1 Okt 19.5505 19.6145 19.5925 19.3710 2 Nov 21.8750 21.7815 21.7995 20.5445 3 Dez 25.4335 25.2230 25.2800 22.7500 t = read.csv("Mappe1.csv", header = TRUE, sep=";", dec = ".", fill = TRUE
month value1 value2 value3 value4
1 Okt 19.5505 19.6145 19.5925 19.3710
2 Nov 21.8750 21.7815 21.7995 20.5445
3 Dez 25.4335 25.2230 25.2800 22.7500
t = read.csv("Mappe1.csv", header = TRUE, sep=";", dec = ".", fill = TRUE, comment.char = "")
t$m <- factor(t$m, levels = c("Okt", "Nov", "Dez"))
library(Hmisc)
xyplot(t$value1~t$m, type = "l", col = "red", ylab="values")
lines(t$value2~t$m, type = "l", col = "cyan")
lines(t$value3~t$m, type = "l", col = "purple")
lines(t$value4~t$m, type = "l", col = "black", lwd = 2)
legend("topleft", legend=c("value1", "value2", "value3", "value4"),
col=c("red", "cyan", "purple", "black"), lty=1:1, cex=0.8)
我已经应用了plot.new()和dev.off()。但有时我仍然会得到这些错误,或者有时R不显示错误,但根本不绘图
这里可能有什么问题
非常感谢您的帮助 如果您想使用ggplot2,下面介绍如何将数据变形为长格式,并使用
ggplot2
进行打印
t <- read.table(text = "month value1 value2 value3 value4
1 Okt 19.5505 19.6145 19.5925 19.3710
2 Nov 21.8750 21.7815 21.7995 20.5445
3 Dez 25.4335 25.2230 25.2800 22.7500", header = TRUE)
t$month <- factor(t$m, levels = c("Okt", "Nov", "Dez"))
library(tidyr)
# "melt" the data into a long format
# -month tells the function to "melt" everything but month
xy <- gather(t, key = variable, value = value, -month)
library(ggplot2)
# for some reason you need to specify group and color to make things work
ggplot(xy, aes(x = month, y = value, group = variable, color = variable)) +
theme_bw() +
geom_line()
t如果您想使用ggplot2,下面介绍如何将数据变形为长格式,并使用ggplot2
进行打印
t <- read.table(text = "month value1 value2 value3 value4
1 Okt 19.5505 19.6145 19.5925 19.3710
2 Nov 21.8750 21.7815 21.7995 20.5445
3 Dez 25.4335 25.2230 25.2800 22.7500", header = TRUE)
t$month <- factor(t$m, levels = c("Okt", "Nov", "Dez"))
library(tidyr)
# "melt" the data into a long format
# -month tells the function to "melt" everything but month
xy <- gather(t, key = variable, value = value, -month)
library(ggplot2)
# for some reason you need to specify group and color to make things work
ggplot(xy, aes(x = month, y = value, group = variable, color = variable)) +
theme_bw() +
geom_line()
t您正在混合栅格(晶格/ggplot2)和基础图形。使用其中一种。您正在混合栅格(晶格/ggplot2)和基础图形。用一个或另一个。哇,太好了,这正是我需要的!非常感谢你!!哇,太好了,这正是我需要的!非常感谢你!!