When plotting graphs with categorical variables (such as boxplots) with long names, the names have to be shifted using the theme command in ggplot2, then the distance between the axis ticks and the text can be set as well yet this distance is reflected on both axis when it is some time only necessary on one axis. Below some sample code:
df<-data.frame(X=rnorm(50,0,10),Y=c(rep("Some Text",25),rep("Some Larger Text That Takes Space",25)))
#classical boxplots
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45))
#the x axis labels need to be shifted downwards
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45),axis.ticks.margin=unit(4,"cm"))
#now they are shifted but there is unnecessary space on the y-axis
How can we set axis.ticks.margin to act on only one axis?
Try this for example :
library(grid)
axis.ticks.margin=unit(c(4,-4),'cm'))
So, the ggplot2 call becomes:
ggplot(df,aes(x=Y,y=X))+
geom_boxplot()+
theme(axis.text=element_text(size=20),
axis.text.x=element_text(angle=45),
axis.ticks.margin=unit(c(4,-4),'cm'))
Related
I made several weekley plots of different gaseous compounds (BVOCs) with ggplot and I put them together with plot_grid function. Obviously compounds have different scales and Y axis are not aligned.
I wish align them by zero on Y axis.
I think that I can avoid to share the dataset and the single plots code, because the point is on plot_grid function that put them together.
Here the plot_grid function that I used:
plot_grid(metmax,acetalmax,formicmax,acetmax,nrow = 4,align = "hv",rel_widths= c(1,1,1,1),rel_heights = c(1.2,1.2,1.2,1.2))
Here an example of how appear my final plot with Y axis out of phase.
If I understand it correctly, you want the line at y=0 to appear at the same height in all plots. That would require them all to have the same range and, by consequence, the same scale.
You can add to your ggplot() call the following:
+ ylim(min_value, max_value)
min_value and max_value can be calculated by the script by looking at the range of values occurring in your plot data.
https://ggplot2.tidyverse.org/reference/lims.html
I am trying to put labels beside some points which are very close to each other on geographic coordinate. Of course, the problem is overlapping labels. I have used the following posts for reference:
geom_text() with overlapping labels
avoid overlapping labels in ggplot2 charts
Relative positioning of geom_text in ggplot2?
The problem is that I do not want to relocate labels but increase the interval of labeling (for example every other 10 points).
I tried to make column as alpha in my dataframe to make unwanted points transparent
[![combined_df_c$alpha=rep(c(1,rep(0,times=11)),
times=length(combined_df_c$time)/
length(rep(c(1,rep(0,times=11)))))][1]][1]
I do not know why it does not affect the plot and all labels are plotted again.
The expected output is fewer labels on my plot.
You can do this by sequencing your dataframe for the labs of geom_text.
I used the build-in dataset mtcars for this, since you did not provide any data. With df[seq(1,nrow(df),6),] i slice the data with 6-steps. This are the labels which get shown in your graph afterwards. You could use this with any steps you want. The sliced dataframe is given to geom_text, so it does not use the original dataset anymore, just the sliced one. This way the amount of points for the labels and the amount of labels are equal.
df <- mtcars
labdf<- df[seq(1,nrow(df),6),]
ggplot()+
geom_point(data=df, aes(x=drat, y=seq(1:length(drat))))+
geom_text(data=labdf,
aes(x=drat, y=seq(1:length(drat))), label=labdf$drat)
The output is as expected: from 32 rows, just 6 get labeled.
You can easily adjust the code for your case.
also: you can put the aes in ggplot() which may be more useful if you use more then just gemo_point. I made it like this, so i can clarify: there is a different dataset used on geom_text()
I gathered three geom_bar() plots in one plot by grid.arrange:
I removed the y axis of the two right plots (Q2,Q3) and kept the y axis of Q1 as a common axis. Subsequently, I change plot.margin a little bit to obtain a continuous x axis. Although this worked fine, it bugs me that the leftmost plot is smaller than the remaining two plots. I highlighted the difference in size by plotting the y axis again. I tried to fix this by changing plot.margin but without success. Is there any way to equalize the size of the three plots?
The package patchwork (https://github.com/thomasp85/patchwork) automatically balances the size of the plots while aggregating the plots
I am trying to plot data with a categorical x-axis variable and a continuous y-axis variable. The current plot is shown below:
A am trying to alter the height of the y-axis to make the plot taller and thinner. I know you can do this kind of thing with a continuous variable vs. continuous variable scatterplot using coord_fixed(), e.g.:
However, this works on the numeric ratios of the x and y data, which doesn't apply if one variable is categorical. Trying coord_fixed on my data with any input seems to just scale the plot exactly to the plot area:
Trying to adjust the canvas at the ggsave phase just adds white space around the plot, rather than changing the plot shape itself, which isn't what I'm looking for either:
ggsave(filename = paste(imgSaveDir,'RT_data_summ.png',sep=""),width=7,height=10)
Any help is appreciated!
I have a very large data frame (2 columns) in terms of records. I have plotted the graph in ggplot2. The X axis is the time and the Y axis is values. Over a specific interval from time 50 to 60, I want to make the ticks increments to be smaller such as (50,51,51,53,...59,60). For the rest of the axis, it is fine to have the ticks incremented by 10. So,I would expect to have X-axis values like :
10,20,30,40,50,51,52,53,54,55,56,57,58,58,60,70,80,90,..190,200.
I know, I can modify the ticks through scale_x_continuous by using breaks and minor_breaks. However, I'm not getting the expected output. Here is my try:(note: have created data just for example as my data is very large).
data:
-----
x<-seq(1:200)
y<-seq(51,250,by=1)
df<-data.frame(x=x,y=y)
code:(Update based on #Didzis Elferts suggestion)
-----
ggplot(data=df, aes(x,y))+geom_line(size=1.6)+
scale_x_continuous( breaks=c(10,20,30,40,seq(50,60,by=2),seq(70,200,10))
,minor_breaks=seq(50,60,by=2) )+
+theme(axis.text.x=element_text(size=16),axis.text.y=element_text(size=16))+
+theme(axis.title.x=element_text(size=16),axis.title.y=element_text(size=16))+
+theme(axis.ticks.x = element_line(size = 1))+xlab("Time")+ylab("value")
+theme(axis.ticks.length=unit(0.8,"cm"))
Based on the suggestion below, the only problem now is the values of X-axis during the interval between 50-60 is not clearly appeared.
Any suggestions to make it clear!!
With argument minor_breaks= you set the minor gridlines. To set numbers under the x axis all number should be provided with argument breaks=.
ggplot(data=df, aes(x,y))+geom_line(size=1.6)+
scale_x_continuous(breaks=c(10,20,30,40,seq(50,60,by=1),seq(70,200,10)),
minor_breaks=seq(50,60,by=1))
For the second problem - you set axis.ticks.x=element_line(size=5) inside theme() - that makes your axis ticks wider so they appear as small rectangles. If you want to make axis ticks longer use axis.ticks.length=.
+theme(axis.ticks.length=unit(0.5,"cm"))