0

I built a bar chart with ggplot2, but I the labels are inverted and don't understand why. Would you have an idea please ?

And how could I add "Number" variable in the tooltips please ?

This my table :

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)

This what I get :

enter image description here

This is the code :

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()     

      })

Thank you very very much for any help.

Mickey_NC
  • 269
  • 1
  • 5
  • 17
  • I get an error from the argument to position_stack, using ggplot 2.1.0. What version of ggplot2 are you using? Maybe an upgrade will fix things... –  Mar 27 '17 at 22:47
  • Thank you for replying dash24, I just upgraded to 2.2.1 version of ggplot2 but I get the same result. I was on the 2.2.0 version. – Mickey_NC Mar 27 '17 at 23:43
  • Works fine for me on the dev versions of both ggplot and plotly; I think I saw some news about reversing bar order to match legends a little while ago. – alistaire Mar 28 '17 at 00:17
  • Thank you for helping alistaire, excuse me but what are dev versions ? Can I install them ? – Mickey_NC Mar 28 '17 at 00:47
  • Probably, using `library(devtools)`, `install_github` –  Mar 28 '17 at 07:12
  • http://stackoverflow.com/questions/14262497/fixing-the-order-of-facets-in-ggplot – mrina713 Mar 30 '17 at 14:44
  • Thank you very much mrina713. – Mickey_NC Apr 11 '17 at 23:52

0 Answers0