r - ggplot 函数来复制绘图
问题描述
我正在尝试使用 ggplot 复制这些图的主题,我在网上搜索以向我展示如何分配,我发现很少有文章讨论在散点图中更改两个变量的颜色,我尝试了以下方法:
d1<-read.csv("./data/games.csv")
p.1<-ggplot(d1, aes(x=cream_rating, y=charcoal_rating)) +
geom_point(aes(color = cream_rating))
p.1 + ggtitle("Rating of Cream vs Charcoal") +
xlab("rating of cream") + ylab("rating of charcoal")+ theme(plot.title = element_text(hjust = 0.5)) + scale_color_manual(
values=c("orange", "green"))
我收到此错误:
ERROR while rich displaying an object: Error: Continuous value supplied to discrete scale
Traceback:
1. FUN(X[[i]], ...)
2. tryCatch(withCallingHandlers({
. if (!mime %in% names(repr::mime2repr))
. stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
. rpr <- repr::mime2repr[[mime]](obj)
. if (is.null(rpr))
. return(NULL)
. prepare_content(is.raw(rpr), rpr)
. }, error = error_handler), error = outer_handler)
3. tryCatchList(expr, classes, parentenv, handlers)
4. tryCatchOne(expr, names, parentenv, handlers[[1L]])
5. doTryCatch(return(expr), name, parentenv, handler)
6. withCallingHandlers({
. if (!mime %in% names(repr::mime2repr))
. stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
. rpr <- repr::mime2repr[[mime]](obj)
. if (is.null(rpr))
. return(NULL)
. prepare_content(is.raw(rpr), rpr)
. }, error = error_handler)
7. repr::mime2repr[[mime]](obj)
8. repr_text.default(obj)
9. paste(capture.output(print(obj)), collapse = "\n")
10. capture.output(print(obj))
11. evalVis(expr)
12. withVisible(eval(expr, pf))
13. eval(expr, pf)
14. eval(expr, pf)
15. print(obj)
16. print.ggplot(obj)
17. ggplot_build(x)
18. ggplot_build.ggplot(x)
19. lapply(data, scales_train_df, scales = npscales)
20. FUN(X[[i]], ...)
21. lapply(scales$scales, function(scale) scale$train_df(df = df))
22. FUN(X[[i]], ...)
23. scale$train_df(df = df)
24. f(..., self = self)
25. self$train(df[[aesthetic]])
26. f(..., self = self)
27. self$range$train(x, drop = self$drop, na.rm = !self$na.translate)
28. f(..., self = self)
29. scales::train_discrete(x, self$range, drop = drop, na.rm = na.rm)
30. stop("Continuous value supplied to discrete scale", call. = FALSE)
我使用了错误的功能,我应该使用哪一个以及如何在中间获得交叉线?
structure(list(rated = c(FALSE, TRUE, TRUE, TRUE, TRUE, FALSE,
TRUE, FALSE, TRUE, TRUE), turns = c(13L, 16L, 61L, 61L, 95L,
5L, 33L, 9L, 66L, 119L), victory_status = structure(c(3L, 4L,
2L, 2L, 2L, 1L, 4L, 4L, 4L, 2L), .Label = c("draw", "mate", "outoftime",
"resign"), class = "factor"), winner = structure(c(2L, 1L, 2L,
2L, 2L, 3L, 2L, 1L, 1L, 2L), .Label = c("charcoal", "cream",
"draw"), class = "factor"), increment_code = structure(c(3L,
7L, 7L, 5L, 6L, 1L, 1L, 4L, 2L, 1L), .Label = c("10+0", "15+0",
"15+2", "15+30", "20+0", "30+3", "5+10"), class = "factor"),
cream_rating = c(1500L, 1322L, 1496L, 1439L, 1523L, 1250L,
1520L, 1413L, 1439L, 1381L), charcoal_rating = c(1191L, 1261L,
1500L, 1454L, 1469L, 1002L, 1423L, 2108L, 1392L, 1209L)), row.names = c(NA,
10L), class = "data.frame")
我尝试了 Stefan 的建议(非常有帮助)并进行了一些修改:
`d1<-read.csv("./data/games.csv")
ggplot(d1, aes(x=cream_rating, y=charcoal_rating)) +
# Map winner on color. Add some transparency in case of overplotting
geom_point(aes(color = winner), alpha = 0.2) +
# Add the cross: Add geom_pints with one variable fixed on its mean
geom_point(aes(x = mean(cream_rating), color = winner), alpha = 0.2) +
geom_point(aes(y = mean(charcoal_rating), color = winner), alpha = 0.2) +
scale_shape_manual(values=c(16, 17)) +
# "draw"s should be dropped and removed from the title
scale_color_manual(values = c(cream = "seagreen4", charcoal = "chocolate3", draw = NA)) +
ggtitle("Rating of Cream vs Charcoal") +
xlab("rating of cream") + ylab("rating of charcoal") + theme_bw() + theme(plot.title = element_text(hjust = 0.5))
我想从图中过滤掉“绘图”,当我将点形状更改为三角形和圆形时,它们似乎没有改变,此外我得到这个错误:
Warning message:
“Removed 950 rows containing missing values (geom_point).”
Warning message:
“Removed 950 rows containing missing values (geom_point).”
Warning message:
“Removed 950 rows containing missing values (geom_point).”
我注意到的另一件事是,我得到的是双十字而不是一个!
解决方案
问题是您将连续变量 ( cream_rating
) 映射到离散色标 ( scale_color_manual
) 上。
由于图像中的图显示只有两种颜色,即我们需要一个离散变量。由于您的数据是关于收视率的,我的猜测是,要实现绘图,您必须
winner
在颜色上进行映射。仍然存在一个问题:s 怎么样draw
。在下面的代码中,我将绘制的颜色设置为 NA,即丢弃绘制。但是你可以随意改变它。从图像中我也猜想一些透明度被用来解决过度绘图。这可以通过
alpha
我设置为 0.6 的参数来实现。关于出现在你的情节中的十字架。
mean
很难说,但我的猜测是,通过将您的评级变量之一固定为它的值,这里的数据被“复制”了两次。如果这个猜测是正确的,我们可以通过两个额外的geom_point
层得到交叉。
library(ggplot2)
d1 <- structure(list(rated = c(FALSE, TRUE, TRUE, TRUE, TRUE, FALSE,
TRUE, FALSE, TRUE, TRUE), turns = c(13L, 16L, 61L, 61L, 95L,
5L, 33L, 9L, 66L, 119L), victory_status = structure(c(3L, 4L,
2L, 2L, 2L, 1L, 4L, 4L, 4L, 2L), .Label = c("draw", "mate", "outoftime",
"resign"), class = "factor"), winner = structure(c(2L, 1L, 2L,
2L, 2L, 3L, 2L, 1L, 1L, 2L), .Label = c("charcoal", "cream",
"draw"), class = "factor"), increment_code = structure(c(3L,
7L, 7L, 5L, 6L, 1L, 1L, 4L, 2L, 1L), .Label = c("10+0", "15+0",
"15+2", "15+30", "20+0", "30+3", "5+10"), class = "factor"),
cream_rating = c(1500L, 1322L, 1496L, 1439L, 1523L, 1250L,
1520L, 1413L, 1439L, 1381L), charcoal_rating = c(1191L, 1261L,
1500L, 1454L, 1469L, 1002L, 1423L, 2108L, 1392L, 1209L)), row.names = c(NA,
10L), class = "data.frame")
ggplot(d1, aes(x=cream_rating, y=charcoal_rating)) +
# Map winner on color. Add some transparency in case of overplotting
geom_point(aes(color = winner), alpha = 0.6) +
# Just a guess to add the cross: Add geom_pints with one variable fixed on its mean
geom_point(aes(x = mean(cream_rating), color = winner), alpha = 0.6) +
geom_point(aes(y = mean(charcoal_rating), color = winner), alpha = 0.6) +
# Should "draw"s be colored or dropped?
scale_color_manual(values = c(cream = "green", charcoal = "orange", draw = NA)) +
ggtitle("Rating of Cream vs Charcoal") +
xlab("rating of cream") + ylab("rating of charcoal")+ theme(plot.title = element_text(hjust = 0.5))
编辑
形状没有显示,因为您错过了
winner
在shape
aes上的映射“错误”只是因为我们将
draw
s 的颜色设置为 NA 而出现的警告。这些是 ggplot 删除的行。要摆脱draw
s 只需在绘图之前过滤您的数据集:
library(ggplot2)
library(dplyr)
d1 %>%
filter(winner != "draw") %>%
ggplot(aes(x=cream_rating, y=charcoal_rating, color = winner, shape = winner)) +
# Map winner on color. Add some transparency in case of overplotting
geom_point(alpha = 0.6, na.rm = TRUE) +
# Just a guess to add the cross: Add geom_pints with one variable fixed on its mean
geom_point(aes(x = mean(cream_rating)), alpha = 0.6) +
geom_point(aes(y = mean(charcoal_rating)), alpha = 0.6) +
# Should "draw"s be colored or dropped?
scale_color_manual(values = c(cream = "green", charcoal = "orange")) +
scale_shape_manual(values = c(cream = 16, charcoal = 17)) +
ggtitle("Rating of Cream vs Charcoal") +
xlab("rating of cream") + ylab("rating of charcoal")+ theme(plot.title = element_text(hjust = 0.5))
推荐阅读
- django - 数据迁移在导入时触发查询
- selenium - Edge浏览器中的无效选择器异常,而在chrome浏览器中可以找到相同的元素而没有任何异常
- java - Android 房间日期
- tensorflow - 如何找到函数最大的值(实现代码)?
- marko - 带有组件的 Marko 动态标签
- laravel - 消息:显示带有变量 User_ID 的用户名
- c# - 如何修复“SQLite 错误 1:'找不到指定的模块。” 使用 EF 和 Spatialite
- node.js - 如何在节点中发出表单数据请求,从 s3 中提取文件
- jetbrains-ide - JetBrains Rider 附加到 w3wp.exe 进程?
- tensorflow - 如何创建特定形状的 Keras ZeroTensor