堆积条形图中的标签顺序ggplot2

时间:2018-02-21 12:53:11

标签: r ggplot2

我用ggplot2做了一个堆积条形图:

BlogRequest

我得到的是错误顺序的百分比值标签:

using an abstract base model class

数据是这样的excel文件:

st <- read.csv("C:/Users/..../1.csv", sep=";")
st$Question <- factor (st$Question, levels=c("d41", "d42", "d43", "d44"))
st$Value <- factor(st$Value, levels = c("Not important", "Of a little importance", "Important", "Very Important"))

ggplot() + geom_bar(aes(y = st$Percentage, x = st$Question, fill = st$Value), data = st,stat="identity")+ coord_flip() + geom_text(data=st, mapping=aes(x=st$Question, y=st$Percentage, label=st$Percentage), size=4, vjust=0.5)+ scale_fill_brewer(palette = 11)

我该如何解决? 我试过

st = matrix( c("Not important",     0.00,   "d41",
         "Of a little importance",  0.00,   "d41",
          "Important",  4.76,   "d41",
          "Very Important", 95.24,  "d41",
          "Not important",  0.00,   "d42",
          "Of a little importance", 0.00,   "d42",
          "Important",  9.52,   "d42",
          "Very Important", 90.48,  "d42",
          "Not important",  0.00,   "d43",
          "Of a little importance", 11.90,  "d43",
          "Important",  52.38,  "d43",
          "Very Important", 35.71,  "d43",
          "Not important",  0.00,   "d44",
          "Of a little importance", 14.29,  "d44",
          "Important",  52.38,  "d44",
          "Very Important", 33.33,  "d44"), nrow=16, ncol=3, byrow=TRUE)
colnames(st) <- c("Value",  "Percentage",   "Question")

position = position_stack(vjust = 0.5)

geom_text(aes(y=pos, label=labels), vjust=0)

没有正面结果。

提前谢谢!

stack

1 个答案:

答案 0 :(得分:0)

p7<-ggplot(st, aes(x = factor(Question), y = Percentage, fill = Value)) + geom_bar(position = position_stack(), stat = "identity", width = .7) + geom_text(aes(label = label), position = position_stack(vjust = 0.5),size = 4) + coord_flip() + scale_fill_brewer(palette = 18)

p7+labs(x="Question", y="Percentage Values", fill="Legend")+ theme(axis.text=element_text(size=15),                                                                                                 axis.title=element_text(size=17, face="bold"),                                                                                                   legend.title=element_text(size=13), legend.text=element_text(size=12))

解决: solved