r - 你能把它翻译成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"))
问题是我什至没有得到我想要的颜色。
感谢阅读和帮助!
解决方案
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()
推荐阅读
- javascript - 在 React 中提交表单后无法清除输入字段
- javascript - 加载图标并在 php 加载时从“shell_exec”结果中获取值
- dc.js - 无法进行交叉过滤
- react-native - 如何在 react-navigation v5 中使用 withNavigation?
- c++ - `decltype(&ordenary_func)` 和 decltype`(ordenary_func)` 的区别
- mysql - Navicat 无法识别 Mac 上 known_hosts 文件中的新条目并且无法连接到远程服务器
- logstash-grok - 如何从字符串中提取关键字值
- angular - 多个 HTML 页面合并为一个页面
- r - 在 R 中对多个数据帧进行汇总统计
- oracle - Oracle Apex 在应用程序中一次导入多个导出的文件