beautiful Pie Charts with R
You can try with the pie3D()
function from the plotrix
package:
library(plotrix)
pie3D(mydata$FR, labels = mydata$group, main = "An exploded 3D pie chart", explode=0.1, radius=.9, labelcex = 1.2, start=0.7)
fancy pie chart in R using ggplot2
You could do:
ggplot(data, aes(x="", y=prop, fill=group)) +
geom_bar(stat="identity", width=10, size = 3, color = "white") +
coord_polar("y", start=0) +
theme_void() +
theme(legend.position="none") +
geom_text(aes(y = ypos, label = paste(group, round(prop,2), sep = "\n")),
color = "white", size=4, nudge_x = 3) +
scale_fill_brewer(palette="Set1")
Plotting multiple Pie Charts with label in one plot
You don't have to supply different y values for geom_text
and geom_bar
(use y = value
for both of them). Next you have to specify position in geom_text
. Finally, remove scales
from facets.
library(ggplot2)
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
ggplot(piec, aes("", value, fill = type)) +
geom_bar(stat = "identity", color = "white", size = 1) +
geom_text(aes(label = paste0(value * 100, "%")),
position = position_stack(vjust = 0.5),
color = "white", size = 3) +
coord_polar(theta = "y") +
facet_wrap(~ title, ncol = 3) +
scale_fill_manual(values = c("#0048cc", "#cc8400")) +
theme_void()
create pie charts with facet wrap?
I am not sure what you want to show, so you could do something like this:
df <- data.frame(rideable_type = c("classic_bike", "classic_bike", "docked_bike", "electric_bike", "electric_bike"),
member_causal = c("causal", "member", "causal", "causal", "member"),
number_of_bikes = c(1313878, 2013712, 308241, 1048194, 1250025),
total = c(2670303, 3263737, 2670303, 2670303, 3263737),
freq = c(49.2, 61.6, 11.5, 39.2, 38.3))
library(ggplot2)
ggplot(df, aes(x = factor(1), fill=factor(rideable_type))) +
facet_wrap(~member_causal) +
geom_bar(width = 1,position = "fill") +
coord_polar(theta="y") +
labs(fill = "rideable type", x = "", y = "")
Created on 2022-07-08 by the reprex package (v2.0.1)
Pie Chart in ggplot2 picking single samples from a dataframe
Your data just seems to be in a non-tidy format. It's easier to plot it if you reshape it. Here's an example using tidyr
library(ggplot2)
library(tidyr)
df %>%
pivot_longer(-Sample) %>%
ggplot() +
aes(x="", y=value, fill=name) +
geom_col(position="fill") +
facet_wrap(~Sample) +
coord_polar("y")
pie chart with ggplot2 with specific order and percentage annotations
You have to change levels of Make
by share
or volume
(provided data is already sorted):
dfc$Make <- factor(dfc$Make, levels = rev(as.character(dfc$Make)))
And play with theme
arguments:
ggplot(dfc[1:10, ], aes("", share, fill = Make)) +
geom_bar(width = 1, size = 1, color = "white", stat = "identity") +
coord_polar("y") +
geom_text(aes(label = paste0(round(share), "%")),
position = position_stack(vjust = 0.5)) +
labs(x = NULL, y = NULL, fill = NULL,
title = "market share") +
guides(fill = guide_legend(reverse = TRUE)) +
scale_fill_manual(values = c("#ffd700", "#bcbcbc", "#ffa500", "#254290")) +
theme_classic() +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, color = "#666666"))
Related Topics
Deploying R Shiny App as a Standalone Application
Add a Horizontal Line to Plot and Legend in Ggplot2
Ggplot2 Multiple Scales/Legends Per Aesthetic, Revisited
How to Coerce a List Object to Type 'Double'
Use Filter in Dplyr Conditional on an If Statement in R
Writing Robust R Code: Namespaces, Masking and Using the '::' Operator
Error in Installation a R Package
Plotting Pca Biplot with Ggplot2
Lme4::Lmer Reports "Fixed-Effect Model Matrix Is Rank Deficient", Do I Need a Fix and How To
Convert Data Frame with Date Column to Timeseries
R: How to Get the Week Number of the Month
Bigrams Instead of Single Words in Termdocument Matrix Using R and Rweka
Counting Number of Instances of a Condition Per Row R
How to Delete Groups Containing Less Than 3 Rows of Data in R