1

I just read lot of questions and answers before asking this. But I didn't found the exact answer, resp. it doesn't work.

I have this data frame:

data frame

UPDATE #rewrited also in R format:

structure(list(abzufr = structure(c(5L, 3L, 2L, 6L, 4L, 5L), .Label = 
c("#NULL!", 
"1", "2", "3", "4", "5", "9"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 2L), class = "factor", .Label = c("überhauptnicht", 
"ehernicht", "teilweise", "eherja", "vollundganz")), value = 
c("9.90990990990991", 
"11.038961038961", "4.98687664041995", "6.55737704918033", 
"9.03225806451613", 
"22.5225225225225")), .Names = c("abzufr", "variable", "value"), class = 
c("data.table", "data.frame"), row.names = c(NA, -6L
), .internal.selfref = <pointer: 0x00000000001f0788>)

I already reshaped this before, so I have the data prepared for doing a bar chart with ggplot2.

By doing this it comes really strange since the numbers on the y axis are not sorted from 0, but somehow unsorted:

ggplot(Percentage.melt, aes(variable, value, fill = abzufr))+
  geom_bar(position = "dodge", stat="identity")+
  scale_x_discrete(labels=c("überhaupt nicht", "eher nicht", "teilweise", "eher ja", "voll und ganz"))+
  scale_fill_brewer(palette ="YlOrRd", name="Arbeitszufriedenheit", labels=c("resignative Zufriedenheit","konstruktive Unzufriedenheit", "stabilisierte Zufriedenheit","fixierte Unzufriedenheit", "progressive Zufriedenheit"))+
  theme_bw()+
  theme(axis.title.y = element_blank(), axis.title.x = element_blank(), axis.text.x = element_text(), axis.text.y = element_text())+
  ggtitle("Bedenken/Sorgen bezüglich organisationalen Abläufen")`

graphic

Before I calculated the percentage of every category by:

library(data.table)
Percentage<-setDT(newdata)[ , .(überhauptnicht = paste0(( nrow(.SD[sil2 == 
"1.0"]) / .N ) * 100), 
  ehernicht = paste0(( nrow(.SD[sil2 == "2.0"]) / .N ) * 100), 
  teilweise = paste0(( nrow(.SD[sil2 == "3.0"]) / .N ) * 100), 
  eherja = paste0(( nrow(.SD[sil2 == "4.0"]) / .N ) * 100),
  vollundganz = paste0(( nrow(.SD[sil2 == "5.0"]) / .N ) * 100))   , by = 
abzufr]

I tried to round the numbers by

library(dplyr) 
Percentage.melt %>% mutate_if(is.numeric, round, digits=2)

and other options discussed here on r and it didn't work. Does anybody know how to round this numbers and make it appear "normal sorted by 0 and higher" on the y axis?

Thankful for every help

Anetta S
  • 23
  • 3
  • 3
    Please post a representative sample of your data as the output of `dput`, not as an image. Otherwise we don't have easy access to it to try reproducing the issue. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – camille Aug 15 '18 at 14:15
  • 3
    I think what's happening is that the numbers you're putting on the y-axis aren't numbers at all. Note that they *are* sorted, but sorted according to the first value (e.g., `y <- c("1.345", "4.67", "2.89", "10.34"); sort(y)`). Make sure those values are numeric (or force them as such with `as.numeric()`) and try again. – Steven Aug 15 '18 at 14:19
  • @camille I just did it, thanks for you notice. – Anetta S Aug 15 '18 at 14:28
  • 1
    You could also use the `type.convert` to change your value to a number, such as `Percentage.melt$value <- type.convert(Percentage.melt$value)` – Kerry Jackson Aug 15 '18 at 14:43
  • @KerryJackson you are a genius! Thank you so much, it worked for me and now the graphic looks really great. – Anetta S Aug 15 '18 at 14:55

0 Answers0