[R] Formatting ggplot2 graph

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Wed Jul 6 15:50:34 CEST 2016


Hi All,

my current code looks lke this:

freq_ls <- structure(list(Var1 = c("zldkkd", "aakdkdk", 
                                   "aaakdkd", "aaieiwo", "vöalsl", 
"ssddkdk", 
                                   "glowowp", "vvvvlaoiw", "ruklow", 
"rolsl", 
                                   "delk", "inslvnz"), Anzahl = c(1772L, 
761L, 
 536L, 317L, 197L, 160L, 30L, 20L, 10L, 6L, 6L, 1L), Prozent = c(46.4, 
                                                             19.9, 14, 
8.3, 5.2, 4.2, 0.8, 0.5, 0.3, 0.2, 0.2, 0)), .Names = c("Var1", 
                                                      "Anzahl", 
"Prozent"), class = c("tbl_df", "data.frame"), row.names = c(NA, 
                                                     -12L))
ggplot(freq_ls) +
  geom_bar(aes(x = Var1,
               y = Anzahl),
           stat = "identity",
           fill = "gray") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  ggtitle("Title of the Plot")

I would like to add the abolute and relative frequencies on top of the 
bars. In addition I want the values printed in descending ording according 
to the data.

I searched the web and found:

geom_text(stat='bin',aes(label=..count..),vjust=-1)

(Source: 
http://stackoverflow.com/questions/26553526/how-to-add-frequency-count-labels-to-the-bars-in-a-bar-graph-using-ggplot2
)

but this does not work in my case. Inserting the code

ggplot(freq_ls) +
  geom_bar(aes(x = Var1,
               y = Anzahl),
           stat = "identity",
           fill = "gray") +
  geom_text(stat='bin',aes(label=..count..),vjust=-1) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  ggtitle("Title of the Plot")

results in

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: Removed 1 rows containing missing values (geom_text). 


I looked in the book Wickhan: ggplot2 but could find an answer to the 
question:

- How to show number if tey are pre-calculated?
- How to sort the bars according to the sequence of values in descending 
order or if - pre-ordered - in the given order?

What do I have to change in my code to do it?

Kind regards

Georg



More information about the R-help mailing list