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"))
Related
I'm analyzing numeric data with values between 1 to 7. I want to plot boxplots and show the significance across categories. My problem is that adding the labels also extends the values in the y axis. This might imply that the possible data range is up to more than 7 - which is not the best. I tried using ylim() but using it cuts off the signif labels. Is there a way to make the axis values to be 1-7, without cutting the information the should apear beyond this range?
my current plot:
when using ylim()
the desired outcome is something like that:
As mentioned in the comments, the solution is setting breaks:
gboxplot(...)+ scale_y_continuous(breaks = seq(0, 7, by = 1))
Say i have a fairly long vector which i want to present as a barplot:
myvec<-runif(2000,0,1)
barplot(myvec, col="grey", border=NA, names.arg = seq(1:2000))
i would like x axis to go pretty:
to have, say 4-5 labels to be shown on the axis, but with the ticks that determine to which specific bar it corresponds.
worst to worst i can live with the random labels that are being picked automatically, but i want to see which bar they correspond to.
thanks
You can try this:
library(ggplot2)
myvec<-data.frame(v1=runif(2000,0,1))
ggplot(myvec,aes(x=dplyr::row_number(v1),y=v1))+geom_bar(stat='identity')
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
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'))