使用x与xyplot绘制月度数据

时间:2016-11-07 07:49:32

标签: 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, 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)

这个例子非常好用。但是当我以相同的方式尝试它但具有不同的值时,只有value1是plottet并且我总是得到以下错误:

Error in plot.xy(xy.coords(x, y), type = type, ...) : 
  plot.new has not been called yet
Error in strwidth(legend, units = "user", cex = cex, font = text.font) : 
  plot.new has not been called yet

我已经应用了plot.new()和dev.off()。但有时候我仍然会遇到这些错误,或者有时R没有显示错误但根本没有绘制错误。

这可能是什么问题?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果你想采用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() 进行绘制。

SELECT 
PlayerID, HeroTypeID, HeroTypeIDCount, Wins / (Losses + Wins) AS WinRate, Wins, Losses
FROM (
    SELECT E.PlayerID AS PlayerID, 
           FK_HeroTypeID AS HeroTypeID, 
           COUNT(FK_HeroTypeID) AS HeroTypeIDCount,
           SUM(CASE WHEN D.Result = 'LOSS' THEN 1 ELSE 0 END) AS Losses, 
           SUM(CASE WHEN D.Result = 'WIN' THEN 1 ELSE 0 END) AS Wins
        FROM GamePlayerDetail D
        JOIN Player E
            ON D.FK_PlayerID = E.PlayerID
        JOIN Game I
                ON D.FK_GameID = I.GameID
        WHERE PlayedDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
    GROUP BY E.PlayerID, FK_HeroTypeID
) AS T
ORDER BY PlayerID

enter image description here