首页 > 解决方案 > 你能把它翻译成ggplot吗?

问题描述

所以基本上,我想使用 ggplot 函数 geom_line + geom_point 来创建相同的图,但图形更漂亮。

> a
          V1        V2        V3 
1  0.8224887 0.7882316 0.7596440 
2  0.7892779 0.7604186 0.7409430 
3  0.8254516 0.8257800 0.8014778 
4  0.8268519 0.7887464 0.7887322 
5  0.8226651 0.7981079 0.7934783 


plot(6:10, a$V1, type="l", xlab="Folds", ylab="Accuracy", col="Blue",ylim=c(0.7,0.9))
par(new=TRUE) 
plot(6:10, a$V2, type="l", xlab="Folds", ylab="Accuracy", col="Orange",ylim=c(0.7,0.9))
par(new=TRUE) 
plot(6:10, a$V3, type="l", xlab="Folds", ylab="Accuracy", col="Green",ylim=c(0.7,0.9))

我的主要目标是获得一个有助于区分每个变量的图例。

我试图只绘制第一行:

ggplot(data = a)+
  theme_classic()+
  geom_line(aes(x=6:10, y = a$V1, color = "blue"))

问题是我什至没有得到我想要的颜色。

感谢阅读和帮助!

标签: rggplot2

解决方案


library (dplyr)
library (ggplot2)

a <- data.frame(
V1=rnorm(5),
V2=rnorm(5),
V3=rnorm(5),
Folds = 6:10) # make some example data

a %>% 
tidyr::gather(key,value,-Folds) %>% #get data in long format for ggplot
ggplot(.,aes(x = Folds,y = value,y,col = key))+
geom_line() + # add line
geom_point() + # add points
scale_color_manual("My Variables",values = c("blue","orange","green")) + #change colours
theme_classic()

推荐阅读