R ggplot2倒置标签在堆积条中的位置

时间:2017-03-27 22:41:17

标签: r ggplot2

我用ggplot2构建了一个条形图,但是标签是倒置的,不明白为什么。请问你有想法吗?

如何在工具提示中添加“数字”变量?

这是我的表:

Area <- c("West", "West", "West", "West", "West", "West", "West", "West", "West", "West", "West", "West", "North", "North", "North", "North", "North", "North", "North", "North", "North", "North", "North", "North", "South", "South", "South", "South", "South", "South", "South", "South", "South", "South", "South", "South")

Household <- c("WC", "WC", "BD", "BD", "CL", "CL", "CES", "CES", "RC", "RC", "LL", "LL", "WC", "WC", "BD", "BD", "CL", "CL", "CES", "CES", "RC", "RC", "LL", "LL", "WC", "WC", "BD", "BD", "CL", "CL", "CES", "CES", "RC", "RC", "LL", "LL")

Answer <- c("Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No")

Number <- c(3300,1411,3988,723,98,4613,163,4548,4005,706,3177,1534,12566,1692,13228,1030,2506,11752,2025,12233,12366,1892,10123,4135,65216,878,65404,690,30737,35357,18497,47597,64131,1963,60244,5850)

Sum <- c(4711, 4711, 4711, 4711, 4711, 4711, 4711, 4711, 4711, 4711, 4711, 4711, 14258, 14258, 14258, 14258, 14258, 14258, 14258, 14258, 14258, 14258, 14258, 14258, 66094, 66094, 66094, 66094, 66094, 66094, 66094, 66094, 66094, 66094, 66094, 66094)

Rate <- c(70.05,29.95,84.65,15.35,2.08,97.92,3.46,96.54,85.01,14.99,67.44,32.56,88.13,11.87,92.78,7.22,17.58,82.42,14.20,85.80,86.73,13.27,71.00,29.00,98.67,1.33,98.96,1.04,46.50,53.50,27.99,72.01,97.03,2.97,91.15,8.85)

df <- data.frame(Area, Household, Answer,Number, Sum, Rate)

这就是我得到的:

enter image description here

这是代码:

ggplot(df, aes(x = Household, y =  Rate, fill = Answer)) +        
        geom_bar(stat = "identity") +
        geom_text(aes(label = paste(Rate, "%")), position = 
position_stack(vjust = 0.5), size = 3) +        
        facet_grid(~Area) +
        scale_fill_manual(values = c("#975900", "#EDB340")) +
        theme(axis.text.x = element_text(size = 6), legend.title = 
element_blank()) +
    theme(axis.text.y = element_text(size = 6), legend.title = 
element_blank()) +
        theme(axis.title = element_text(family = "Trebuchet MS", color = 
"#000000", size = 7)) +
        theme(legend.text = element_text(size = 8)) +
        labs(list(title = "", x = "", y = "Rate"))

        output$plot <- renderPlotly({  
        ggplotly()     

      })

非常感谢您的帮助。

0 个答案:

没有答案