【问题标题】:Plot coloured boxes around axis label围绕轴标签绘制彩色框
【发布时间】:2019-06-11 21:16:17
【问题描述】:

考虑这个简单的例子

library(tidyverse)
tibble(x = as.factor(c('good', 'neutral', 'bad')),
       y = as.factor(c('bad', 'neutral', 'bad'))) %>% 
  ggplot(aes(x = x, y = y)) + geom_point()

我想将 x 标签(goodneutralbad)放在不同颜色的框中。例如,good(在 x 轴和 y 轴上)将被一个小绿色框包围,依此类推。

我可以在ggplot2 中这样做吗?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

像这样?

tibble(x = as.factor(c('good', 'neutral', 'bad')),
y = as.factor(c('bad', 'neutral', 'bad'))) %>%
ggplot(aes(x = x, y = y)) + 
  geom_point() + 
  theme(axis.text.x = element_text(color = c('red', 'blue', 'green')))

你的阴谋:

编辑

使用grid 的另一种漂亮的 Ghetto 解决方案

tibble(x = as.factor(c('good', 'neutral', 'bad')),
y = as.factor(c('bad', 'neutral', 'bad'))) %>%
ggplot(aes(x = x, y = y)) + 
  geom_point()

grid::grid.polygon(x = c(.3,.3,.25,.25), y = c(.07,.04,.04,.07),gp = gpar(col = 'green', fill = 'green', alpha = .5))
grid::grid.polygon(x = c(.525,.525,.575,.575), y = c(.07,.04,.04,.07),gp = gpar(col = 'red', fill = 'red', alpha = .5))
grid::grid.polygon(x = c(.79,.79,.86,.86), y = c(.07,.04,.04,.07),gp = gpar(col = 'blue', fill = 'blue', alpha = .5))

【讨论】:

  • 是的,非常接近!除了不给字母上色,我想在每个标签周围放一个小彩色框。说一个坏的红色框,一个好的绿色,一个中立的 whie
  • 我们快到了。只是在kablehaozhu233.github.io/kableExtra/awesome_table_in_html.htmlhaozhu233.github.io/kableExtra/awesome_table_in_html.html中寻找非常像背景spec_color的标签
  • 谢谢!很有趣,但我不明白这里的grid 步骤
  • 例如,我怎样才能得到 y 轴上的彩色框?
  • 是的,你可以。网格将整个绘图输出“理解”为图形,而不使用轴。 X 和 Y 坐标是轴标签的绝对位置,因此您只需稍微调整一下它们,直到您的框覆盖您的 y 标签。这相当于使用 MS Paint 完成绘图并在其上绘图;)
【解决方案2】:

在情节区域外使用geom_label的解决方案:

ggplot(data, aes(x, y)) +
  geom_point() +
  geom_label(aes(0.3, y, label = y, fill = y), hjust = 0) +
  geom_label(aes(x, 0.45, label = x, fill = x)) +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    legend.position = "none"
  ) +
  coord_cartesian(xlim = c(1, 3), ylim = c(1, 2), clip = "off")


另一种解决方案

您应该创建geom_rect 有边框,但没有填充并将它们绘制在绘图区域之外(使用coord_cartesian):

library(tidyverse)
data <- tibble(
  x = as.factor(c('good', 'neutral', 'bad')),
  y = as.factor(c('bad', 'neutral', 'bad'))
)
ggplot(data, aes(x, y)) +
  geom_point() +
  # put rects on y-axis
  geom_rect(aes(xmin = 0.1, xmax = 0.45, color = y,
                ymin = as.numeric(y) - 0.1, ymax = as.numeric(y) + 0.1),
            fill = NA, size = 3) +
  # put rects on x-axis
  geom_rect(aes(ymin = 0.3, ymax = 0.4, color = x,
                xmin = as.numeric(x) - 0.15, xmax = as.numeric(x) + 0.15),
            fill = NA, size = 3) +
  # Here it's important to specify that your axis goes from 1 to max number of levels
  coord_cartesian(xlim = c(1, 3), ylim = c(1, 2), clip = "off")

【讨论】:

  • 看起来不错!但我确实想要一个填充物。就像下面的解决方案。我们可以这样做吗?此外,这里的盒子超大。我们可以让它们更适合文本吗?
  • @ℕʘʘḆḽḘ 我添加了另一个解决方案
  • 哈!!!看起来好多了...除了标签在情节上...如果我们以某种方式避免这样做,那么我们就有了解决方案!
  • @ℕʘʘḆḽḘ 添加了中性主题的快速解决方案
  • 谢谢。我认为这行得通。我有机会挑选自己的颜色吗? :)
【解决方案3】:

另一种方法

创建一个颜色向量并将它们传递给theme()axis.text.x() 选项。

# data
x = as.factor(c('good', 'neutral', 'bad'))
y = as.factor(c('bad', 'neutral', 'bad'))
df<- data.frame(x,y)

# create a vector of colors
mycolors<- c("red","blue","green")
library(ggplot2)
ggplot(df, aes(x = x, y=y))+
  geom_point()+
  theme(axis.text.x = element_text(colour = mycolors))

【讨论】:

【解决方案4】:

一种方法可能是这样的:

tibble(x = as.factor(c('good', 'neutral', 'bad')),
       y = as.factor(c('bad', 'neutral', 'bad'))) %>% 
  ggplot(aes(x = x, y = y)) + geom_point()+
  geom_rect(aes(xmin=0.5, xmax=1.5, ymin=-Inf, ymax=Inf), fill="red", alpha=0.1)+
  geom_rect(aes(xmin=1.5, xmax=2.5, ymin=-Inf, ymax=Inf), fill="yellow", alpha=0.1)+
  geom_rect(aes(xmin=2.5, xmax=3.5, ymin=-Inf, ymax=Inf), fill="green", alpha=0.1)

使用geom_rect(),您可以添加彩色背景:

【讨论】:

  • 啊!谢谢,但我的意思是标签。比如说,bad 包含在一个红色框中,但这仅发生在轴上。好像我们只是想为轴标签设置颜色,但这次我们用一个小的彩色框围绕标签(好、坏、中性)而不是为文本着色。你明白我的意思吗?
  • 我不知道如何将标签放入彩色盒子中。但是你可以调整标签文本的颜色:+ theme(axis.text.x = element_text(color=c("red","yellow","green")), axis.text.y = element_text(color=c("orange","blue")))
猜你喜欢
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多