0

我正在准备一系列情节,使用 sjPlot 包。对于简单的频率演示,我使用 sjp.frq。我想为每个条使用不同的颜色。我找到了选择颜色的选项,但它仅适用于整个系列:开关 geom.colors 允许更改所有条的颜色。即使组合 geom.colors=c("color1","color2","color3") 也不起作用。

是否有任何解决方案可以实现类似的效果:

data(mpg)

sjp.frq(mpg$year,title = "", axis.title = "", 
        show.prc = TRUE, show.n = FALSE,
        show.axis.values = FALSE)
4

1 回答 1

0

I'm not sure, but I think I recall that ggplot2 used this color scheme by default for plots, if no color aesthetics was specified. However, later versions of ggplot now use a single color for simple frequencies (without grouping/colour aesthetics):

library(ggplot2)
library(sjmisc)
data(efc)
ggplot(efc, aes(e42dep)) + geom_bar()

enter image description here

That's why the image you posted has different colors, while now sjp.frq prints bars in one color only. Since you don't have a grouping aesthetics for simple frequency bars, you can't provide different colors for each geom / bar in sjp.frq. In this case, you have to find your own solution and add a group-aes, like:

ggplot(efc, aes(e42dep, fill = to_label(e42dep))) + 
  geom_bar() + 
  labs(y = NULL, x = get_label(efc$e42dep), fill = get_label(efc$e42dep)) +
  scale_x_continuous(breaks = c(1:4), labels = get_labels(efc$e42dep))

enter image description here

However, to me it does not make much sense to give each bar a seperate color and provide axis labels. Using a legend instead of axis labels (drop axis labels) would work, but this makes the graph less intuitive, because you have to switch between legend and bars to find out which bar represents which category. For simple frequency plots, this is unnecessary complexity.

于 2016-06-22T10:13:13.667 回答