Remove "floating" axis labels in facet_wrap plot? - r

Using facet_wrap in ggplot2 to create a grid, but I have an uneven number of panels so the last row is incomplete. At the bottom of the last, blank panel is the axis ticks and text. Is it possible to shift this axis up (giving the last facet in each column the appearance of having applied free_x)? If not, can I remove it altogether as is seen below?
To clarify with examples, this is what I'm getting:
http://sape.inf.usi.ch/sites/default/files/ggplot2-facet-wrap.png
I desire something seen here (though, ideally with axis labelling on the facet in column 4):
Changing facet label to math formula in ggplot2
Thanks for any ideas or insight!

Using facet_wrap, when I do this in 0.9.1, ggplot hides the x-axes on the columns with blanks, as shown below.
movies$decade <- round(movies$year, -1)
ggplot(movies) + geom_histogram(aes(x=rating)) + facet_wrap(~ decade, ncol=5)

Related

How to increase the interval of labels in geom_text?

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()

ggplot2: Can I add the strip labels to every plot with facet_grid()?

Here is a typical case for using multiple variables in the rows or columns in facet_grid().
My question is how can I add the strip labels to every plot? For instance, the second row only contains the strip label "f" in the y-axis. I need all six plots all get two strip labels in X and Y.
I am new to ggplot2 and R, I hope you can help me. Thank you all so much.

Keep grid lines and (optional) tick marks when using breaks ggplot

I'm creating a line graph with ggplot that makes use of breaks to display only some of the labels. The issue is when I do that, the non-labeled tick marks and more importantly, the grid lines associated with those tick marks disappear. I really need the grid lines to remain. The tick marks staying would be nice, but not essential.
The code below will produce a graph that shows what I'm dealing with.
Thanks!
count<-c(1,5,6,8,3)
period<-c(1,2,3,4,5)
df<-data.frame(count,period)
library('ggplot2')
ggplot(data=df, aes(x = period, y =count ,group=1)) +
geom_line(size=1)+
scale_x_discrete(breaks=c(1,3,5),
labels=c("One",'Two','Three'))

Set ticks margin on one axis (ggplot2)

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'))

R bar chart colours for groups of bars

Fairly new to R so sorry if this is a dumb question.
I want to plot a bar chart of a lot of data - maybe 100 bars.
I want to use colours and spacing to highlight the "groups", so I might have the first 10 bars in blue, a small gap, the next 20 in red, a small gap and so on.
I can plot the data fine, but how can I do the colouring and gaps in this way?
This can be done quite easily with ggplot2 as provided in links by #Arun.
With base graphics to set space between bars you can use argument space= (sets space before each bar) and argument col= will change color in function barplot().
Here is a example with 20 bars and space between each 5 bars.
df<-sample(1:10,20,replace=T)
barplot(df,space=c(0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0),
col=rep(c("red","blue","green","yellow"),each=5))
If the number of observations in each group is identical then you can convert vector of values to matrix and then plot it (with argument beside=TRUE). In this case you just need to supply colors but bars will be grouped automatically.
df2<-matrix(df,ncol=4)
barplot(df2,beside=TRUE,col=rep(c("red","blue","green","yellow"),each=5))

Resources