我正在尝试使用ggplot2::geom_bar
使用背景阴影(使用ggplot2::geom_rect()
)根据分类变量绘制堆积的条形图,如下所示:
shading <- data.frame(min = seq(from = 0.5, to = max(as.numeric(as.factor(diamonds$clarity))), by = 1),
max = seq(from = 1.5, to = max(as.numeric(as.factor(diamonds$clarity))) + 0.5, by = 1),
col = c(0,1))
ggplot() +
theme(panel.background = element_rect(fill = "transparent")) +
geom_bar(data = diamonds, mapping = aes(clarity, fill=cut)) +
geom_rect(data = shading,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
fill = factor(col), alpha = 0.1)) +
geom_bar(data = diamonds, mapping = aes(clarity, fill=cut)) +
guides(alpha = FALSE)
如何更改阴影的颜色?
我尝试了scale_fill_manual(values = c("white", "gray53"))
,但似乎在ggplot2
(https://github.com/hadley/ggplot2/issues/578)中无法实现多尺度美学。还有另一种方法可以获得理想的结果吗?
答案 0 :(得分:6)
是的,不要将您的颜色放在aes()
中,而是将它们放在外面(因此它们按原样使用)。由于它不在aes()
来电之内,因此您必须使用明确的fill=shading$col
而不是fill=col
,并且shading$col
应该具有您所追求的颜色名称(而不是shading$col <- ifelse(shading$col, 'white', 'gray53')
ggplot() +
theme(panel.background = element_rect(fill = "transparent")) +
geom_bar(data = diamonds, mapping = aes(clarity, fill=cut)) +
geom_rect(data = shading,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf, alpha = 0.1),
fill=shading$col) + # <-- here
geom_bar(data = diamonds, mapping = aes(clarity, fill=cut)) +
guides(alpha = FALSE)
而不是被解释为因素的变量。)
<?php
$image = get_field('palette');
if( !empty($image) ):
?>
<div class="small-12 large-12 columns center">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</div>
<?php endif; ?>