Hi supposed I have the following pie info.
df=structure(list(Var1 = c("a", "b"), Freq = c(306L, 1064L), per = c(0.223357664233577,
0.776642335766423)), row.names = c(NA, -2L), class = "data.frame")
and I plot this pie chart with direction -1
ggplot(data = df,
aes(x = "", y = per, fill = Var1))
geom_col()
geom_text(aes(label = scales::percent(per, accuracy = 1)),
position = position_stack(vjust = 0.5),
color = "grey20", size = pietext )
coord_polar(theta = "y", direction =-1 )
theme_void ()
However what I would like is to label with with the frequency rather than the percentage.
Var1 Freq per
1 a 306 0.2233577
2 b 1064 0.7766423
Is this possible with this method? I tried replacing per with Freq but that just gave some really strange text.
thank you.
CodePudding user response:
aes(label = Freq) is the way to go:
ggplot(data = df,
aes(x = "", y = per, fill = Var1))
geom_col()
geom_text(aes(label = Freq),
position = position_stack(vjust = 0.5),
color = "grey20", size=12)
coord_polar(theta = "y", direction =-1)
theme_void ()
CodePudding user response:
Just change geom_text(aes(label =Freq)
ggplot(data = df, aes(x = "", y = per, fill = Var1))
geom_col()
geom_text(aes(label =Freq),
position = position_stack(vjust = 0.5),
color = "black", size = 10 )
labs( x="", y="", fill="")
scale_fill_manual(values=c("#4E79A7", "#fc7d0b"))
coord_polar(theta = "y", direction =-1 )
theme_no_axes()
Plot:
or something that is better than a pie chart
Sample code:
library(waffle)
library(ggthemes)
vals <- c(306,1064)
val_names <- sprintf("%s (%s)", c("a","b"), label=vals)
names(vals) <- val_names
waffle::waffle(vals)
ggthemes::scale_fill_tableau(name=NULL)
Plot:
Sample code:
df=structure(list(Var1 = c("a", "b"), Freq = c(306L, 1064L), per = c(0.223357664233577, 0.776642335766423)), row.names = c(NA, -2L), class = "data.frame")