【问题标题】:remove empty outline in ggplot bar chart删除 ggplot 条形图中的空轮廓
【发布时间】:2019-04-23 09:51:33
【问题描述】:

我有一个这样的数据框:

df <- data.frame(time = rep(1:10, 2),
                 value = c(0,0,0,0, abs(rnorm(6)),
                           0,0,0,0,0, abs(rnorm(5))),
                 group = c(rep("B", 10),
                         rep("A", 10)),
                 group_fill = c(rep("no", 7),
                              rep("B", 3),
                              rep("no", 7),
                              rep("A", 3)) )

我将其绘制为堆积条形图:

ggplot(df, aes(x = time, y = value, color= group, fill = group_fill)) + 
  geom_bar(stat = "identity") +
  scale_color_manual(values=c("#E69F00", "#56B4E9", "#333333")) +
  scale_fill_manual(values=c("#E69F00", "#56B4E9", "#FFFFFF")) +
  scale_x_continuous(breaks = 1:10) +
  theme_bw()

从数据集中可以明显看出,“B”的前 5 个观测值和“A”的前 4 个观测值正好为零。

但是,ggplot 为这些值添加了一点橙色轮廓。

如何去除前 1:5 观察中“A”的橙色轮廓?

我希望时间 5-7 没有填充,即只有轮廓,这一点很重要。

第二个问题是如何使“组”图例充满蓝色和橙色而不是灰色?

【问题讨论】:

  • 为什么不过滤掉所有值都为零的时间?
  • 要替换图例填充,您可以使用scale_color_manual(values=c("#E69F00", "#56B4E9", "#333333"), guide = guide_legend(override.aes = list(fill = c("#E69F00", "#56B4E9")))

标签: r ggplot2 bar-chart


【解决方案1】:

经过编辑以将零转换为 NA,这似乎有效。

df <- data.frame(time = rep(1:10, 2),
                 value = c(0,0,0,0, abs(rnorm(6)),
                           0,0,0,0,0, abs(rnorm(5))),
                 group = c(rep("B", 10),
                           rep("A", 10)),
                 group_fill = c(rep("no", 7),
                                rep("B", 3),
                                rep("no", 7),
                                rep("A", 3)) )

 df[df == 0] <- NA

  ggplot(df, aes(x = time, y = value, color = group, fill = group_fill)) + 
  geom_bar(stat = "identity") +
  scale_color_manual(values=c("#E69F00", "#56B4E9", "#333333"), guide = F) +
  scale_fill_manual(values=c("#E69F00", "#56B4E9", "#FFFFFF")) +
  scale_x_continuous(breaks = 1:10) +
  theme_bw()

【讨论】:

  • 我忘了补充说我想要轮廓,就像我有的一样,只有零不应该有轮廓
【解决方案2】:

将数据框设置为子集以排除零 subset(df, value != 0),并删除带有 guide = FALSE 的颜色图例:

set.seed(1)
df <- data.frame(time = rep(1:10, 2),
                 value = c(0,0,0,0, abs(rnorm(6)),
                           0,0,0,0,0, abs(rnorm(5))),
                 group = c(rep("B", 10),
                         rep("A", 10)),
                 group_fill = c(rep("no", 7),
                              rep("B", 3),
                              rep("no", 7),
                              rep("A", 3)) )    
    ggplot(subset(df, value != 0), aes(x = time, y = value, color= group, fill = group_fill)) +
          geom_bar(stat = "identity") +
          scale_color_manual(values = c("#E69F00", "#56B4E9", "#333333"),  guide = FALSE) +
          scale_fill_manual(values = c("#E69F00", "#56B4E9", "#FFFFFF")) +
          scale_x_discrete(breaks = 1:10) +
          theme_bw()

【讨论】:

  • 谢谢,但时间点 5-7 应该与它们的轮廓完全一样,只有零点应该是不可见的,没有轮廓
【解决方案3】:

如果您还想在没有任何条形线的情况下显示空白时间,则可以使用 base barplot。首先,我们需要宽格式的数据,可以使用reshape(您也可以查看?data.table::dcast)并创建两个克隆来实现。

dat1 <- reshape(dat[-4], idvar="group", direction="wide")
dat1[] <- lapply(dat1, function(x) if (all(x == 0)) NA else x)  # set zeros to NA
dat1.1 <- dat1.2 <- dat1  # create clones

在一个克隆中,我们将“no times”设置为NA,而在另一个克隆中则相反。

no.times <- paste0("value.", 1:7)

dat1.1[names(dat1) %in% no.times] <- NA
dat1.2[-which(names(dat1) %in% no.times)] <- NA

现在我们可以使用bordercol 的不同设置来绘制一个图(使用选项add=TRUE)。

barplot(as.matrix(dat1.1[-1]), names.arg=1:10, 
        ylim=c(-.1, max(colSums(dat1[-1]), na.rm=TRUE) + .1),
        border=0, col=c("#56B4E9", "#E69F00"), 
        main="My plot", xlab="Time", ylab="Value")
barplot(as.matrix(dat1.2[-1]), names.arg=1:10,
        border=c("#56B4E9", "#E69F00"), col=NA, add=TRUE)
box()  # add box around plot
legend("topleft", legend=c("A", "B", "no"), pch=c(15, 15, 0),  # add legend
       col=c("#56B4E9", "#E69F00", "black"), title="Group")

 

结果

数据

dat <- structure(list(time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), value = c(0, 0, 0, 
0, 1.37095844714667, 0.564698171396089, 0.363128411337339, 0.63286260496104, 
0.404268323140999, 0.106124516091484, 0, 0, 0, 0, 0, 1.51152199743894, 
0.0946590384130976, 2.01842371387704, 0.062714099052421, 1.30486965422349
), group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor"), 
    group_fill = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("A", 
    "B", "no"), class = "factor")), class = "data.frame", row.names = c(NA, 
-20L))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2020-06-14
    • 1970-01-01
    • 2020-01-16
    • 2016-09-25
    相关资源
    最近更新 更多