Reputation: 1
My goal is to create a bar graph of categorical variables and then add a pie chart inside it as shown in the attached image.
my data is the same as in the image and is below:
#For bar grapgh
chromosomes = c("1A", "1B", "1D", "2A", "2B", "2D", "3A", "3B", "3D", "4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668, 752, 213, 826, 948, 334, 625, 834, 264, 488, 391, 136, 745, 917, 234, 543, 848, 182, 901, 740, 241)
data_bar <- data.frame(chromosomes, Frequency)
#For pie chart
Genome = c("A","B","D")
Count = c(4796, 5430, 1604)
data_pie <- data.frame(Genome, Count)
I will highly appreciate if anyone can guide me or direct me towards where I can find the answers
Upvotes: 0
Views: 354
Reputation: 174293
Here's a ggplot2
solution:
pie <- ggplot(data_pie, aes(x = 1, y = Count, fill = Genome)) +
geom_col(color = "black") +
scale_fill_manual(values = c("red2", "forestgreen", "dodgerblue3")) +
coord_polar(theta = "y") +
theme_void() +
theme(legend.position = "none")
ggplot(data_bar) +
geom_col(aes(chromosomes, Frequency, fill = substr(chromosomes, 2, 2)),
color = "black", width = 0.5) +
scale_fill_manual(values = c("red2", "forestgreen", "dodgerblue3")) +
theme_classic() +
theme(legend.position = "none") +
annotation_custom(ggplotGrob(pie), xmin = "2B", xmax = "6A", ymin = 500)
Upvotes: 1
Reputation: 2977
Using only base R functions, please see the code below:
## Your data ---------------------------------
#For bar grapgh
chromosomes = c("1A", "1B", "1D", "2A", "2B", "2D", "3A", "3B", "3D", "4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668, 752, 213, 826, 948, 334, 625, 834, 264, 488, 391, 136, 745, 917, 234, 543, 848, 182, 901, 740, 241)
#For pie chart
Genome = c("A","B","D")
Count = c(4796, 5430, 1604)
## One idea to start with --------------------
plot.new()
par(mfrow = c(2, 1), # set the layout, you might also consider layout() or split.screen() as suggested in ?par
mar=c(4, 4, 1, 1), # this set up give enough space to see axis labels and titles
oma=c(0, 0, 0, 0)) # remove outer margins
pie(Count, labels = Genome)
barplot(Frequency~chromosomes)
Output:
I think it is possible to make it look cleaner adjusting par()
arguments but I am not very familiar with them.
There are also packages cowplot
and gridExtra
that works nicely with ggplot2
.
Upvotes: 0