Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将表格放在ggplot下面,并使用与plot相同的分组因子为行着色_R_Ggplot2_Plot - Fatal编程技术网

R 如何将表格放在ggplot下面,并使用与plot相同的分组因子为行着色

R 如何将表格放在ggplot下面,并使用与plot相同的分组因子为行着色,r,ggplot2,plot,R,Ggplot2,Plot,我想在我的绘图下面放一个表格,但我希望行的颜色与绘图中组的颜色相匹配(使用“预测器”作为颜色的公共分组因子)。有人能帮我想出如何按组给桌子上的行涂颜色吗?下面是一个例子: d <- data.frame(USArrests) d$predictor <- factor(c(rep(c(1, 2), times = 25))) library(dplyr) label <- d %>% group_by(predictor) %>% filter(Assa

我想在我的绘图下面放一个表格,但我希望行的颜色与绘图中组的颜色相匹配(使用“预测器”作为颜色的公共分组因子)。有人能帮我想出如何按组给桌子上的行涂颜色吗?下面是一个例子:

d <- data.frame(USArrests)
d$predictor <- factor(c(rep(c(1, 2), times = 25)))

library(dplyr)
label <- d %>% 
  group_by(predictor) %>%
  filter(Assault == max(Assault))

table <- data.frame(label$predictor)
table$measure1 <- c(1.45, 5.67)
table$measure2 <- c(4.55, 6.11)
table.p <- ggtexttable(table, rows = NULL,
                        theme = ttheme("mOrange"))

library(ggplot2)
a <- ggplot(d, aes(x=UrbanPop, y=Assault, fill=predictor)) +
  geom_col(position=position_dodge(width = 0, preserve = "single"), width = 5) +
  geom_text(data = label, aes(label = UrbanPop), vjust = -0.5)

ggarrange(a, table.p,
          ncol = 1, nrow = 2,
          heights = c(1, 0.4))
d这将起作用

table.p <- ggtexttable(table, rows = NULL,
                       theme = ttheme(colnames.style = colnames_style(color = "Black", fill = "grey"),
                                      tbody.style = tbody_style(color = "black", fill = as.factor(d$predictor))))

library(ggplot2)
a <- ggplot(d, aes(x=UrbanPop, y=Assault, fill=predictor)) +
  geom_col(position=position_dodge(width = 0, preserve = "single"), width = 5) +
  scale_fill_manual(values=as.factor(d$predictor))+
  geom_text(data = label, aes(label = UrbanPop), vjust = -0.5)

ggarrange(a, table.p,
          ncol = 1, nrow = 2,
          heights = c(1, 0.4))

table.p在我看来,您应该自己选择颜色,并通过适当的使用选项(不使用r,所以我将其留在这里)在表格和绘图上使用它们@NikosM。谢谢,不过你知道如何在表格中按行(或按第一列中的值)分配颜色吗?我不使用r,但我想应该有一些选项可以控制颜色,并且可以由用户程序分配。有没有一种方法可以将这两种颜色都作为自动颜色比例来分配,以便它们可以采用更多的预测值?你可以设置,fill=as.factor(d$predictor)在这两个地方。我将很快更新我的答案。