Position of Labels and Position/Size of Title String

27 views
Skip to first unread message

Georg

unread,
Mar 16, 2017, 10:19:49 AM3/16/17
to ggplot2
Hi All,

I am new to ggplot and tried this:


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

How to I Position the percentages in a more general way depending on the size of pie chunks, e. g. 25%, 25%, 50%?

How do I resize the font for the title and how can I center it in the middle of the plot?

I am looking forward to your reply.

Kind regards

Georg


Brandon Hurr

unread,
Mar 16, 2017, 1:51:51 PM3/16/17
to Georg, ggplot2
Georg, 

You can supply a vector of position adjustments to move individual percentages using either hjust or vjust (or both) to get the look you want. 

In your theme call there is a plot.title element that you are changing the size. Size = 4 is quite small depending on your other settings. Just adjust it there and use hjust to center it. 

Here is an example:

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, vjust=c(2,10,-10)) +
  labs(title = "Pie Title") + theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5))

Typical hjust and vjust values are between -1 and 1, but you can go higher or lower to suit your desires. 

HTH
B

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Georg

unread,
Mar 21, 2017, 9:49:41 AM3/21/17
to ggplot2
Hi Brandon,

many thanks for your reply.

I followed your suggestion using vjust and hjust and got a plot as follows:

ggplot(df,
       aes(

         x = "",
         y = value,
         fill = group)) +
  geom_bar(
    width = 1,
    stat = "identity") +
  scale_fill_brewer(
    name = "Gruppe",
    palette = "Blues") +
  blank_theme +
  geom_text(
    aes(
      y = c(10, 40, 75),
      label = scales::percent(value/100)),
    size = 5,
    vjust=c(3, 1.0, 0.5),
    hjust=c(0, 1.9, 0.7)) +
  labs(title = "Pie Title") +

  coord_polar("y", start = 0) +
  theme(plot.title = element_text(

    size = 10,
    face="bold",
    hjust = 0.5)) +
  guides(fill = guide_legend(reverse = TRUE))

I found the values for vjust and hjust by playing around. This process is rather tidious. Is there a way to find the right values right away or a formula that I could use?

Kind regards

Georg

Brandon Hurr

unread,
Mar 21, 2017, 11:01:18 AM3/21/17
to Georg, ggplot2
Georg, 

I'm sure you could figure this out, but it will take some time. 

The position of the labels is based upon the size of that portion and the order of the factors. So, if you take those into account, you can probably figure out some relationship that would tell you how to adjust the position better to your eyes. 
I would do a google search and look on stackoverflow and see if someone hasn't figured it out before. 

HTH, 
B

Reply all
Reply to author
Forward
0 new messages