1

I'm trying to create a very simple pie chart using ggplot2. The proportions are incorrect as the data I have for three "type" categories is: "M-types" = 7, "N-types" = 151, "E-types" = 57 (see below for output of dput() to generate ).

N and Chronotype are my column headers as defined by colnames(). Here is the core of my code:

pie = ggplot(df, aes(x="", y=N, fill=Chronotype))+
  geom_bar(width = 1, stat = "identity")

pie = pie + 
  coord_polar("y", start=0)

Erroneous pie chart

Data from dput():

structure(list(N = structure(c(3L, 1L, 2L), .Label = c("151", 
    "57", "7"), class = "factor"), Chronotype = structure(c(2L, 3L, 
    1L), .Label = c("E-type", "M-type", "N-type"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -3L))
Bradford
  • 107
  • 1
  • 1
  • 9
  • 2
    Use `y=as.numeric(as.character(df$N))` or change your N from a factor to a numeric. https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information – Jon Spring Nov 14 '19 at 17:20

1 Answers1

1

Thanks to Jon, I just made the y data numeric:

pie = ggplot(df, aes(x="", y=as.numeric(as.character(N)), fill=Chronotype))+
  geom_col() #(width = 1, stat = "identity")

pie = pie + 
  coord_polar("y", start=0)

pie
markhogue
  • 1,056
  • 1
  • 6
  • 16