我想单独控制多面图中面板和绘图的背景颜色。在中使用提示 this answer ,我能够创建一些几乎可以工作的东西,但仍然有两个问题:
到目前为止,这是我的代码:
ann_text <- data.frame(mpg = c(22, 15, 30),
wt = c(4, 5, 2),
cyl = factor(c(4, 6, 8), levels = c("4","6","8")),
lab = c("Text 1", "Text 2", "Text 3"),
hue = c("pink", "yellow", "lightblue"))
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = hue),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point()
result of my code with 'Strange' colors
我本来想在 theme() 中使用类似于下面这行的内容,但是当然(?!)这行不通:
plot.background = element_rect(data = ann_text, mapping = aes(fill = hue))
有什么想法可以让我有机会指定面板和绘图的背景颜色?
最佳答案
使用scale_fill_identity
。
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = hue),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point() +
scale_fill_identity()
或者,您可以将美学包装在 I
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = I(hue)),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point()
https://stackoverflow.com/questions/64423420/
相关文章:
python - 使用 python 控制包的错误奈奎斯特图
azure - Terraform 对动态资源的 ignore_changes
javascript - 使用 React 根据另一个下拉列表更新选择的选项
json - Angular environment.ts 问题 JSON。然而 environme
android - 如何从 Android 应用程序内部了解用户谷歌播放国家/地区?
google-sheets - Google 表格中的 ArrayFormula 和 "AND"公式
MongoDB Atlas 搜索 - 搜索字符串中的多个术语具有 'and' 条件(不是 'or'