this is my first time to ask questions in stack overflow.
Recently I have done a bit of data visualization on the Amino Acids by making a multiple pie charts
First, this is my dataset
library(ggplot2)
df = data.frame(Species <-c('Chicken','Chicken','Chicken','Chicken','Chicken','Human','Human','Human','Human','Human','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Mouse','Mouse','Mouse','Mouse','Mouse','Zebrafish','Zebrafish','Zebrafish','Zebrafish','Zebrafish'),
Amino_acids <- c('E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others'),
value <- c(18,6,10,9,57,26,14,8,5,46,29,15,10,4,42,23,17,7,5,48,31,4,13,7,46))
df$Species <- factor(df$Species)
df$Amino_acids <- factor(df$Amino_acids)
Then, using ggplot2 to make the data visualization
ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
geom_bar(width = 1, stat = "identity", position= "fill") +
coord_polar("y", start=0)+
facet_grid(.~ Species) + facet_wrap(.~Species, strip.position="top")+theme_void()
and the result is this
Results](https://i.stack.imgur.com/8EheY.png)
The problem is I dont know why some of the group (species) titles are being cut off, for example, crab-eating macaque, that is very confusing to me. Also, I have tried hjust on the axis.title.x and y already and it seems no use at all.
Can anyone solve this issue for me? I would rly appreciate it if you can.
You're looking to edit strip.title.x. You have several ways to go around this. I'd prefer editing the box size for the text:
ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
geom_bar(width = 1, stat = "identity", position= "fill") +
coord_polar("y", start=0)+
facet_grid(.~ Species) +
facet_wrap(.~Species) +
theme_void() +
theme(strip.text.x = element_text(margin = margin(1,0,1,0), "cm"))
However, you could also adjust the vertical position:
ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
geom_bar(width = 1, stat = "identity", position= "fill") +
coord_polar("y", start=0)+
facet_grid(.~ Species) +
facet_wrap(.~Species) +
theme_void() +
theme(strip.text.x = element_text(vjust = 1))
Both should result in this:
I created this bar chart using ggplot. I had to create my own count function and called it 'a,' but now the label for that axis just has an a and nothing else... How do I fix it?
a <- count(df, glass)
gl <- ggplot(df, aes(x=glass, y="a", fill=glass)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
xlab("Glass type") +
ylab("Count") +
coord_flip() +
theme_minimal() +
theme(legend.position = "none")
gl
Here is my graph
The default stat value for geom_bar is “count”, which means that geom_bar() uses stat_count() to count the rows of each x value, or glass in this case. Using stat="identity" will override the default geom_bar() stat and require that you provide the y values in order for aggregation to occur. I don't believe you are trying to do this.
Try the following and see if it is what you were looking for:
gl <- ggplot(df, aes(x=glass, fill=glass)) +
geom_bar() +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
xlab("Glass type") +
ylab("Count") +
coord_flip() +
theme_minimal() +
theme(legend.position = "none")
I have the following ggplot2 codes running in R. I need to tweak the codes so that the bars of each FY value are next to each other rather than being stacked.
My codes stand as follows:
p1 <- ggplot(dff3, aes(x=Gender, fill=FY)) + ggtitle("Gender") +
xlab("Gender") +
geom_bar(aes(y = 100*(..count..)/sum(..count..)), width = 0.5) +
ylab("Percentage") +
coord_flip() +
theme_minimal() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
p1
The plot looks like this:
rently like this:
You can use position_dodge() within geom_bar(). Here is an example using mtcars dataset:
library(tidyverse)
ggplot(mtcars, aes(x=factor(am), fill=factor(vs))) +
ggtitle("Gender") +
xlab("Gender") +
geom_bar(aes(y = 100*(..count..)/sum(..count..)), width = 0.5, position = position_dodge()) +
ylab("Percentage") +
coord_flip() +
theme_minimal() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
I am generating a bar chart from data that is in the following format:
count | month
------|-----------
1000 | 2012-01-01
10000 | 2012-02-01
I am trying to assign continuous colors to my bar chart. I want a gradient of colors to be used as the fill color of the bars.
I am trying this:
ggplot(userData, aes(month, count)) +
geom_bar(stat = "identity") +
scale_x_date() +
# scale_fill_gradient(low="blue", high="red") +
scale_fill_continuous(low="blue", high="red", limits=c(0,6500000)) +
labs(x= "Time", y="Count")
However, I am not getting the desired result and the bars in the chart still stays gray as can be seen below:
I have tried with both scale_fill_continuous, and scale_fill_gradient, but without success.
I am not sure exactly what I am missing here.
You haven't assigned the fill aesthetic to anything: aes(month, count, fill = count) should do the trick.
Complete code:
ggplot(userData, aes(month, count, fill = count)) +
geom_bar(stat = "identity") +
scale_x_date() +
scale_fill_continuous(low="blue", high="red") +
labs(x= "Time", y="Count")
(limits are probably useless now, but feel free to add them back)
How to get rid of all this space where the blue lines are?
Data:
data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1),
value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000))
data$is_repeat = factor(data$is_repeat, levels = c(0,1),
labels = c("One-time", "Repeat"))
Plot:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat = "identity", width = 0.3) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
theme_minimal()
edit: I looked at that question and it did NOT solve my problem. My guess is that in the other question's plot, there are 4 bars, so it looks filled. I want to reduce the total width of the X axis.
another edit: Added data.
If you are looking to remove the space between the bars completely and you don't mind the width of bars you could do it with:
geom_bar(stat="identity", position="stack", width=1)
or theme(aspect.ratio=1)
And to remove the space from the end of the plot to the bars you need
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat"))
So your code looks like this:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat="identity", position="stack", width=1) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) +
theme_minimal()
And the output:
You can add space between bars with changing the width=1