ggplot-如何手动调整标签

时间:2018-07-19 08:31:22

标签: r ggplot2 geom-text

我设法用ggplot绘制了“在某些世界地区共同开发的产品”。 我想将计数(而不是百分比)用作标签。 效果很好,但是某些区域的条形图变得如此之细,以至于无法读取数字(请参见图的右下部分)。 我想不列出这些数字或只显示一个数字(求和来自份额极低的区域的计数)。 我该如何手动调整呢?

ggplot(data1_,aes(x = year, y = products_developed_abroad_, 
                   fill = co_region)) + 
  geom_bar(position = "fill",stat = "identity") +
  scale_y_continuous(labels = percent_format()) +
  guides(fill=guide_legend(title="Co-Region")) +
  labs(x="Year", y="Percentage") +
  theme_economist() + scale_fill_manual(values=colors) +
    geom_text(aes(label=products_developed_abroad_),position=position_fill(vjust=0.5), size = 2)

enter image description here

2 个答案:

答案 0 :(得分:1)

Without the data to test the solution it is a bit hard to be sure, but I would test something around the line of:

min_display <- 0.05
+ geom_text(aes(label=products_developed_abroad_, alpha = products_developed_abroad_ > min_display
    ),position=position_fill(vjust=0.5), size = 2) + 
scale_alpha_discrete(range = c(0,1))

That should do the trick to leave them out, even if it's not elegant. Summing them is a bit more difficult, and may also be very confusing.

答案 1 :(得分:0)

解决方案如下:

curl -sL https://raw.githubusercontent.com/home-assistant/hassio-build/master/install/hassio_install \
    | bash -s – -m raspberrypi3

我们唯一要做的就是更改geom_text:

#first we need to calculate shares of co-region by region
data1_$shares <- data1_$products_developed_abroad_/with(data1_, ave(products_developed_abroad_, list(year), FUN = sum)) 
#we need to choose a cutoff value for the shares and set counts associated with lower shares to missing, in this case we want counts with only shares larger than 0.02 (then the remaining counts seem to be displayed properly
data1_$large_bar <- ifelse(data1_$shares > 0.02, data1_$products_developed_abroad, "")