This question already has answers here:
Plot axis name on a different position other than the middle of the axis
(3 answers)
Closed 2 days ago.
The axis titles are normally centered over the axis themselves. How can I change the positions? For example, I want my y-axis title to be on top of the y-axis, and the x-axis title to be on the right of the x-axis. Does anybody know how to do it in R with ggplot2?
Ok, just figured it out. So, for the y-axis, you should set axis.title = element_text(hjust = 1) to be on top, and for the x-axis axis.title = element_text(vjust = 1) to be on the right (which sounds pretty unintuitive to me, given the y-axis is the vertical one). All inside theme(), this is. So, to conclude:
ggplot(data = data, aes(x = x, y = y)) +
geom_point() +
theme(axis.title = element_text(vjust = 1, hjust = 1))
Related
This question already has answers here:
Add percentage labels to a stacked barplot
(2 answers)
R ggplot2 stacked barplot, percent on y axis, counts in bars
(1 answer)
Stacked barplot using R base: how to add values inside each stacked bar
(2 answers)
Closed 2 months ago.
I try to add percentages on the percentage histogram, my code is as follows, but it seems that something is wrong, I can't add the percentages to the graph correctly, and I can't get the perfect percentage histogram, please tell me what is wrong.
data<-data.frame(type=c("A","B","C"),
loss=c(1.7,2.2,2.5,0.8,3.1,4.7,0.5,1.5,1.7,0.7,1.4,1.7),
label=c("1","2","3","4"))
data<-data%>%
group_by(type)%>%
mutate(count=sum(loss))%>%
mutate(freq=round(100*loss/count,2))
ggplot(data,aes(label,loss ,fill=type))+
geom_bar(stat="identity",position="fill",alpha = 0.9)+
theme_bw() + theme(panel.grid=element_blank())+
theme(axis.ticks.length=unit(0.5,'cm'),
legend.position = "top")+
scale_y_continuous(labels = scales::percent)+
geom_text(label = paste0(data$freq,"%"))
Your group_by variable must be "label".
Don't multiply variable with 100. Multiply this variable inside paste() function.
Use position_stack() function to center labels.
library(tidyverse)
data<-data.frame(type=c("A","B","C"),
loss=c(1.7,2.2,2.5,0.8,3.1,4.7,0.5,1.5,1.7,0.7,1.4,1.7),
label=c("1","2","3","4"))
data<-data%>%
group_by(label)%>%
mutate(count=sum(loss))%>%
mutate(freq=round(loss/count,2))
ggplot(data,aes(label,freq ,fill=type))+
geom_bar(stat="identity",position="fill",alpha = 0.9)+
theme_bw() + theme(panel.grid=element_blank())+
theme(axis.ticks.length=unit(0.5,'cm'),
legend.position = "top")+
scale_y_continuous(labels = scales::percent)+
geom_text(label = paste0(100*data$freq,"%"), position = position_stack(vjust = 0.5))
In geom_text(), you need to specify position = position_fill(). You will probably also want to adjust the heights so that the text appears in the middle of each bar, which you can do with vjust:
ggplot(data,aes(label,loss ,fill=type))+
...
geom_text(label = paste0(data$freq,"%"),
position = position_fill(vjust = 0.5))
This question already has answers here:
Remove space between plotted data and the axes
(3 answers)
Closed 1 year ago.
I am trying to move my y-axis tick marks closer to my heatmap.
Here is the code for the heatmap:
ggplot(p_typ_meta, aes(SampleType_Basic, Phylum, fill= Counts)) +geom_tile()+
scale_fill_gradient2(low="blue", mid="white",high="red",midpoint=.15)+theme(axis.title.x = element_text(size=13),axis.title.y = element_text(size=13),axis.text = element_text(size=11),axis.text.y=element_text(margin = margin(0,0)),legend.title.align=0.5, legend.title = element_text(size=13),legend.text = element_text(size=11),plot.title = element_text(hjust=0.5, size=15)) +labs(x="Sample Type", y="Microbial Phyla", title="Microbial Phyla & Sample Type",fill="Relative Abundance")+theme_classic()
I have tried the following:
theme(axis.text.y=element_text(hjust=0.5))
theme(axis.text.y=element_text(margin = margin(0,0)))
theme(axis.ticks.margin=unit(0,'cm'))
theme(axis.text.y = element_text(margin = margin(r = 0)))
scale_y_discrete(expand = c(0, 0))
scale_y_continuous(expand=c(0, 0))
Is there something I am missing in my code? Or is this just how heatmaps work in ggplot2?
I have attached the heatmap I made, and the red arrows show where I want to move the y axis labels. Thanks for the insight!
Using scale_x_discrete(expand = c(0,0)) does the trick. Since you want to reduce the space on the x-axis you need to use this scale and not the one for the y axis.
Consider the following
d = data.frame(y=rnorm(120),
x=rep(c("bar", "long category name", "foo"), each=40))
ggplot(d,aes(x=x,y=y)) +
geom_boxplot() +
theme(axis.text.x=element_text(size=15, angle=90))
The x-axis labels are aligned by the center of the label. Is it possible to automatically align on the right so that every label would end right below the graph?
This is precisely what the hjust and vjust parameters are for in ggplot. They control the horizontal and vertical justification respectively and range from 0 to 1. See this question for more details on justifications and their values (What do hjust and vjust do when making a plot using ggplot?).
To get the labels the way you want you can use:
hjust = 0.95 (to leave some space between the labels and the axis)
vjust = 0.2 (to center them in this case)
ggplot(d,aes(x=x,y=y)) + geom_boxplot() +
theme(axis.text.x=element_text(size=15, angle=90,hjust=0.95,vjust=0.2))
Alternatively, flip the axis, your customers will thank you and have less neck pain (plus, I find most boxplots easier to interpret with this orientation):
ggplot(d, aes(x = x, y = y)) +
geom_boxplot() +
coord_flip()
When creating a geom_histogram in ggplot, the bin labels appear directly underneath the bars. How can I make it so that they appear on either side of the bin, so that they describe the range of each bin (so that the bin that includes cases from 0 to 10 will appear between the 0 and 10 labels)?
I tried using
geom_histogram(position=position_nudge(5))
However, the histogram I'm using is stacked (to differentiate categories within each bin), and this effect is ruined when I add this position. Is there another way of doing it? Maybe moving the axis labels themselves instead of the bars?
Reproducible code:
dd<-data.frame(nums=c(1:20,15:30,40:55),cats=c(rep("a",20),rep("b",30),rep("c",2)))
ggplot(dd, aes(nums))+geom_histogram(aes(nums,fill=cats),dd,binwidth = 10)
results in this:
I want the bars to be shifted to the right by 5, so that the 0 aligns with the left-hand side of the histogram
You can try to define breaks and labels
n <- 10
ggplot(dd, aes(nums, fill=cats)) +
geom_histogram(binwidth = n, boundary = 0) +
scale_x_continuous(breaks = seq(0,55,n), labels = seq(0,55, n))
The following moves the labels of the axis. I wasn't sure how to move the ticks on the x axis so I removed them.
ggplot(dd, aes(nums))+geom_histogram(aes(nums),dd,binwidth = 10)+
theme(axis.text.x = element_text(hjust = 5),
axis.ticks.x = element_blank())
This question already has answers here:
How to move or position a legend in ggplot2
(4 answers)
Closed 7 years ago.
I have created the following heatmap. If you notice that the legend for cohort is on the right and the vertically placed.
How do I move the legend to the bottom in order to give more space for X axis variable month M0 to M55...Also, you will notice that X axis elements are overlapping hence not clear.
Output of the graph:
cohort.clients<-df1
cohort.clients$cohort<-as.character(cohort.clients$cohort)
#we need to melt data
cohort.chart.cl <- melt(cohort.clients, id.vars = 'cohort')
colnames(cohort.chart.cl) <- c('cohort', 'month', 'clients')
#define palette
reds <- colorRampPalette(c('light green',"dark green","yellow"))
#plot data
p <- ggplot(cohort.chart.cl, aes(x=month, y=clients, group=cohort))
p + geom_area(aes(fill = cohort)) +
scale_fill_manual(values = reds(nrow(cohort.clients))) +
ggtitle('Customer Cohort')
Try something like:
ggplot(cohort.chart.cl, aes(x=month, y=clients, group=cohort))
geom_area(aes(fill = cohort)) +
scale_fill_manual(values = reds(nrow(cohort.clients))) +
ggtitle('Customer Cohort') +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.direction = "horizontal", legend.position = "bottom"))
It's also worth noting that your color palette is essentially the same color. If you make cohort$month a factor then ggplot should automatically give you a much more informative palette by default. That being said, with >50 categories, you're well past the realm of a distinguishable colors and might also consider binning the months (into yearly quarters?) and returning to a spectrum like you have now.