[R] Formatting ggplot2 graph

Ulrik Stervbo ulrik.stervbo at gmail.com
Wed Jul 6 17:26:23 CEST 2016


Hi Georg,

Your problem with the geom_text was that you tried to count Var1 in some
bins which cannot be defined on characters - the counts you really wanted
to use are in Anzahl. This should do what you want:

library(ggplot2)

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

# Calculate relative frequencies
freq_ls$rel.freq <- freq_ls$Anzahl/sum(freq_ls$Anzahl)

# Create a string for the plot
freq_ls$bar.text <- paste(freq_ls$Anzahl, round(freq_ls$rel.freq * 100,
digits = 2), sep ="/\n")

# Set the order
freq_ls <- freq_ls[order(freq_ls$Anzahl, decreasing = TRUE), ]
freq_ls$Var1 <- factor(freq_ls$Var1, levels = freq_ls$Var1)

ggplot(freq_ls) +
aes(x = Var1, y = Anzahl) +
  geom_bar(stat = "identity", fill = "gray") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  geom_text(aes(label=bar.text),vjust=0) +
  ggtitle("Title of the Plot") +
  coord_cartesian(ylim=c(0, 2000))

I have move the x and y to the global aesteathics so I won't have to repeat
when adding the geom_text.

I have created an extra column with the strings you want to print on the
plot rather than creating it on the fly.

If no order is predefined ggplot created one by calling factor which
results is some alphabetic order. I therefore first sort the table and set
the factors of Var1 to be the order in the table.

The coord_cartesian  adjusts the y-axis to show the label for the first
column.

Best
Ulrik


On Wed, 6 Jul 2016 at 15:53 <G.Maubach at weinwolf.de> wrote:

> 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
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list