6

我正在尝试使用条形图中的geom_bar每个类别的值来绘制堆叠的条形图,并对其进行标记。由于某些类别的值较小,因此条形图的相应段的高度有时很小。所以,我正在尝试使用geom_text.
我试图定义一个向量,称为size,它根据我试图绘制的变量的值而变化,但是,尽管标签的大小在类别之间确实有所不同,但它似乎与值无关。另外,我不确定为什么我会在图表的右侧获得标签大小的图例。
这是我正在使用的代码的剥离版本:

library(ggplot2)
library(plyr)
library(scales)

Var1 = as.factor(rep(c("A", "B", "C", "D", "E", "F"),2))
Var2 = as.factor(rep(c("Y1","Y2"),each=6))
Freq = c(0.4, 0.1, 0.3, 0.1, 0.05, 0.05,0.2,0.2,0.3,0.2,0.05,0.05)
Data = data.frame(Var1, Var2, Freq)
Data <- ddply(Data, .(Var2), mutate, y = cumsum(Freq)-Freq/2)

size = ifelse(Data$Freq > 0.05, 10, 3)
label = paste(round(Data$Freq*100,0),"%", sep = "")
p = ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) +
  geom_bar(stat = "identity",position = "fill", width = 1) +
  scale_fill_brewer(palette = 3) +
  facet_grid(facets = . ~ Var2) +
  geom_text(aes(y = y, label = label, 
                position ="identity", face = "bold"), size = size, hjust=0.5, vjust=0.5) +
  xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') +
  theme(axis.text.y = element_text(size=14,face="bold"),
        panel.background = element_blank(),
        plot.title = element_text(size = 20, colour = "black", face = "bold"))
p

据我所知,这个问题是由方面引起的,因为这个稍微简化的版本(即没有方面)工作正常:

library(ggplot2)
library(plyr)
library(scales)

Var1 = as.factor(c("A", "B", "C", "D", "E", "F"))
Freq = c(0.4, 0.1, 0.3, 0.1, 0.05, 0.05)
y = cumsum(Freq)-Freq/2
Data = data.frame(Var1, Freq, y)

size = ifelse(Data$Freq > 0.05, 10, 3)
label = paste(round(Data$Freq*100,0),"%", sep = "")
p = ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) +
  geom_bar(stat = "identity",position = "fill", width = 1) +
  scale_fill_brewer(palette = 3) +
  geom_text(aes(y = y, label = label, 
                position ="identity", face = "bold"), size = size, hjust=0.5, vjust=0.5) +
  xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') +
  theme(axis.text.y = element_text(size=14,face="bold"),
        panel.background = element_blank(),
        plot.title = element_text(size = 20, colour = "black", face = "bold"))
p
4

1 回答 1

2

通过在您的数据框中添加一个size并在您的geom_text aes()` 中移动这些参数,这对我来说似乎没问题。我认为..

Data$size <- size

p <- ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) +
       geom_bar(stat = "identity",position = "fill", width = 1) +
       scale_fill_brewer(palette = 3) +
       geom_text(aes(y = y, label = label, 
                     position ="identity", face = "bold", size = size), hjust=0.5, 
                     vjust=0.5) +
       xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') +
       theme(axis.text.y = element_text(size=14,face="bold"),
             panel.background = element_blank(),
             plot.title = element_text(size = 20, colour = "black", face = "bold")) + 
             facet_grid(facets = . ~ Var2) + 
             guides(size=FALSE)

此外,如果您+ guides(size=FALSE)像我一样添加到末尾,这将删除您的尺寸图例。

我对此的解释可能是错误的,一旦你分面,你仍然提供一个完整的长度size,并且不允许分面根据Var2.

我认为你的尺寸问题是size你只有两个尺寸,你会得到很大的差异(可能必须有一些相对缩放),也许添加+ scale_size(range=c(6,10))并使用它以获得更合适的东西?尺寸范围对6,10我来说看起来好多了。

在此处输入图像描述

于 2013-12-27T10:17:24.303 回答