我有一个像这样的数据框:
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的时间不填满,即仅是轮廓。
第二个问题是如何使“组”图例充满蓝色和橙色而不是灰色?
答案 0 :(得分:2)
进行了编辑,可以将零变成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()
答案 1 :(得分: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()
答案 2 :(得分:0)
如果您还想显示空的时间而没有任何条线,则可以使用底数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
现在,我们可以使用add=TRUE
和border
的不同设置在一个图上绘制另一个图(使用选项col
)。
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))