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

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

Related

R ggplot Legend shows shapes inccorectly

Is there a reason why ggplot might mess with the geom_point shape in the legend?
In the actual plot everything looks with the shapes correctly plotted as circles, but in the legend it shows them as weird boxes / squares, i.e. it is showing this:
But it should show this:
Could it be because I have an ifelse in my geom_point ? This is what I have here for this part:
geom_point(aes( y=y, colour=ifelse( (ty>308)&(Time < chron(times=c('08:30:30.0'))), ifelse(side=='left', 'red', 'blue'),'gray')), na.rm = T)
This issue is actually because geom_point and geom_line are both plotted and the points are varying according to the size parameter. ggplot is trying to show a point on a line which looks good when it is small and clear but becomes strange and box-like as the size varies.
To make it clearer, turning off the legend for either the line or the points will keep just one.
For example:
geom_line(aes(y=foo , colour='green'), show.legend = F)

spplot() - ploting two graphs with the same color scale and correcting the position of tick marks

I'm having a problem with the color bar (or color ramp palette) in spplot (adehabitatHR package). I'm want to plot two graphs in the same window using spplot and I also want that the color bar has the same scale for both graphs (just one bar for both graphs). If that is not possible - or too complicated, since I'm new in R language - I wanna set an equal number of tick marks in the bar for both graphs. I had setted this already, but they are in different positions in the color bar - it would make more sense if i have at least one tick mark at lowest value of the bar, one in the middle, and other in the highest valeu. But when I use the colorkey=list(tick.number=3) argument it doesn't allow me to choose where I want the tick marks. How can I do that?
Here is part of my code.
####loading packages....
library("adehabitatHR")
library(latticeExtra)
###... and data
area.total.mari<- read.table("mari.total.txt")
area.total.mari2<- SpatialPoints(area.total.mari)
area.total.mari.mpc=mcp(area.total.mari2)
dry.mari<- read.table("pontos meses seca.txt")
dry.mari1<- SpatialPoints(dry.mari)
dry.mari1.mpc=mcp(dry.mari1)
###color used in the graphs
mycolor2=gray(16:0/16)
##generating kernels and spplot's
ks<-kernelUD(seca.mari1, grid=300, extent=0.2)
ps<-spplot(ks, col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T))+
layer(sp.polygons(area.total.mari.mpc))
kc<-kernelUD(chuva.mari1, grid=300, extent=0.35)
pc<-spplot(kc,col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T)))+
layer(sp.polygons(area.total.mari.mpc))
###printing graphs
######## Tick marks are messed up :/
print(ps, position=c(0,0,.5,1),more=T)
print(pc, position=c(0.5,0,1,1))
Here is the graph generated with this code:
https://www.flickr.com/photos/129526227#N02/16316924041/
Note the problem with the tick marks: the lowest value ("0") is ok, but I can't place the another value in the highest!
This is an example with the "meuse" data set:
library(sp)
library(lattice)
library(adehabitatHR)
library(latticeExtra)
data(meuse)
coordenadas<-data.frame(meuse$x,meuse$y)
attach(coordenadas)
coord.sp.pt<- SpatialPoints(coordenadas)
coord.sp.pt.mpc=mcp(coord.sp.pt)
mycolor=gray(0:12/12)
coord.sp.ud<-kernelUD(coord.sp.pt, grid=300, extent=0.1)
coord.printing<-spplot(coord.sp.ud, col.regions=colorRampPalette(c("white","gray","black"))
,colorkey=list(tick.number=2),scales=list(draw=T))+
layer(sp.polygons(coord.sp.pt.mpc))
print(coord.printing)
Thanks in advance folkes

Expand plotting area by x-axis to make room for direct labels

I am polishing my graphs and have a problem with fitting direct labels in the plotting area. A want to remove most of the area between y1 and the y-axis to the left in the plot similar to that generated by the code below, but keep the extra area to the right to have room for the labels.
Adding +scale_x_discrete(expand=c(0,0.05)) removes extra area on both sides, but leaves no room for labels, and it can't seem to remove it on only one side.
Adding margins to the right of the plotting area with +theme(plot.margin = unit(c(0,4,0,0), "cm")) still doesn't allow the labels to appear there.
A solution that places the labels outside, to the right of the border would be even better.
Any help much appreciated.
library(ggplot2)
library(directlabels)
library(reshape2)
theme_set(theme_bw())
# some data
dfr<-data.frame(c("Longish Name A","Longish Name B","Longish Name C"),c(1,1,1),c(1,2,3),c(2,3,4))
colnames(dfr) <- c("subject","y1","y2","y3")
dfr<-melt(dfr, id.vars="subject")
# the graph
ggplot(data=dfr,aes(y=value, x=variable, group=subject)) +
geom_line(aes(color=subject))+
geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) +
guides(color=FALSE)
Convert your x values to numeric inside the aes() and then use scale_x_continuous() to get back to original labels and set limits= that are wider on side.
ggplot(data=dfr,aes(y=value, x=as.numeric(variable), group=subject)) +
geom_line(aes(color=subject))+
geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) +
guides(color=FALSE)+
scale_x_continuous(breaks=c(1,2,3),labels=c("y1","y2","y3"),expand=c(0,0.05),
limits=c(1,3.4))

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

Remove "floating" axis labels in facet_wrap plot?

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)

Resources