[R] ggplot2: Adjusting title and labels

Ulrik Stervbo ulrik.stervbo at gmail.com
Thu Mar 16 19:04:12 CET 2017


Hi Georg,

If you remove the coord_polar, you'll see that the optimal y-value for the
labels is between the upper and lower bound of the stacked bar-element.

I am not sure it is the most elegant solution, but you can calculate them
like this:

df <- data.frame(group = c("Male", "Female", "Child"),
             value = c(25, 25, 50))

# Order the data.frame to match that of the final plot
df <- df[order(df$group, decreasing = TRUE), ]
# Get the upper bound of the stacked bar element
df$upper <- cumsum(df$value)
# And the lower
df$lower <- c(0, df$upper[seq_along(1:(nrow(df) - 1))])

# Now calculate the position
df$label_pos <- (df$upper - df$lower)/2 + df$lower

# And plot
blank_theme <- theme_minimal() + theme(
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  axis.text.x = element_blank(),
  panel.border = element_blank(),
  panel.grid = element_blank(),
  axis.ticks = element_blank(),
  plot.title = element_text(size = 4, face = "bold"))

ggplot(df, aes(x = "", y = value, fill = group)) +
  geom_bar(
    width = 1,
    stat = "identity")+
  # coord_polar("y", start = 0) +
  scale_fill_brewer(
    name = "Gruppe",
    palette = "Blues") +
  blank_theme +
  geom_text(
    aes(
      y = label_pos,
      label = scales::percent(value/100)),
    size = 5) +
  labs(title = "Pie Title")

HTH
Ulrik


On Thu, 16 Mar 2017 at 17:24 <G.Maubach at weinwolf.de> wrote:

> Hi All,
>
> I have a question to ggplot 2. My code is the following:
>
> -- cut --
>
> library(ggplot2)
> library(scales)
>
> df <-
>   data.frame(group = c("Male", "Female", "Child"),
>              value = c(25, 25, 50))
>
> blank_theme <- theme_minimal() + theme(
>   axis.title.x = element_blank(),
>   axis.title.y = element_blank(),
>   axis.text.x = element_blank(),
>   panel.border = element_blank(),
>   panel.grid = element_blank(),
>   axis.ticks = element_blank(),
>   plot.title = element_text(size = 4, face = "bold"))
>
> ggplot(df, aes(x = "", y = value, fill = group)) +
>   geom_bar(
>     width = 1,
>     stat = "identity") +
>   coord_polar("y", start = 0) +
>   scale_fill_brewer(
>     name = "Gruppe",
>     palette = "Blues") +
>   blank_theme +
>   geom_text(
>     aes(
>       y = c(10, 40, 75),
>       label = scales::percent(value/100)),
>     size = 5) +
>   labs(title = "Pie Title")
>
> -- cut --
>
> Is there a way to give the position of the labels to the chunks of the pie
> in a generalized form instead of finding the value interatively by
> trial-n-error?
>
> How can I adjust the title of the graph converning font height and postion
> (e. g. center)?
>
> Kind regards
>
> Georg
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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