R ggplot reverse axes and control direction of bars - r

I am trying to mimic two types of plots (two images provided) where the negative values are to the right and positive to the left. I have sample-code below:
tdf<-data.frame(prcnt=c(-50,25,-80,5,10,-40),nm=c('AB','BC','CD','DE','EF','FG'),catg=c(rep('catA',2),rep('catB',2),rep('catC',2)))
ggplot(tdf,aes(nm,prcnt,fill=catg))+geom_col()+scale_y_continuous(limits=c(-100,100))+coord_flip()+scale_y_reverse()
How can I also have it mean-centered (at 0) like this? Thanks.

You could get the top plot with something like
ggplot(tdf,aes(ymin=as.numeric(nm)-.45,ymax=as.numeric(nm)+.45,
xmin=100, xmax=prcnt,fill=catg))+
geom_rect() +
scale_y_continuous(breaks=as.numeric(tdf$nm),
labels=levels(tdf$nm))+
scale_x_reverse(limits=c(100, -100))
and you can get the bottom plot with
ggplot(tdf,aes(nm,prcnt,fill=catg))+
geom_col()+
scale_y_reverse(limits=c(100,-100))+
coord_flip()

Related

ggplot2 - how to move geom_rug to avoid overriding points

I have created a basic scatterplot with ggplot2 and added a rug with geom_rug on the y-axis (left side) to it, however the rug is hiding a few points. I have tried position_dodge, position_jitterdodge and hjust in order to move the rug and make those points visible, but it did not give the desired result. I can't move the rug to the right side of the plot, since it will be hiding points here as well.
Is there a way to move the rug on the other side of the y-axis, outside of the plot?
Here is some example code:
data("midwest", package = "ggplot2")
ggplot(midwest, aes(x=area, y=poptotal)) + geom_point(alpha=0.8)+
geom_rug(aes(x=NULL, y=poptotal), alpha=0.8)
I solved the problem by simply expanding the x-axis limits, using
scale_x_discrete(expand = c(0.01,0.01))

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)

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

Multiple plot in the same figure

I have several data and I need to plot them compactly in a picture like this:
I already tried par() layout() and ggplot() but plots are displayed so far each other.
I need them to be very close, as if they were in the same plot with a different y (e.g. plot1 y=0, plot2 y=1, plot3 y=3 and so on..)
Can someone help me?
That can be acquired using the layout, also, but maybe an easier approach is to set the graphical parameters in a suitable way.
Function par() let's you specify the number of panels in a single figure using the argument mfrow. It takes a vector of two numbers, that specify the number sub-figure rows and columns. For example, c(2,1) would create two rows of figure,s but only a single column. That's what is in your example figure. You can change the number of figure rows to the number of sub-figures you would like to plot vertically.
In addition, the margins around each sub-figure can be set using the argument mar. The margins are specified in the order of 1. bottom, 2. left, 3. top., and 4. right. Making the bottom and top margins smaller would draw your sub-figures closer together.
In R this could look something like the following:
# Simulate some random data
a<-runif(10000)
b<-runif(10000)
# Open a new plot windows
# width: 7 inches, height: 2 inches
x11(width=7, height=1)
# Specify the number of sub-figures
# Specify the margins (top and bottom are 0.1, left and right are 2)
# Needs some experimenting with to get these right
par(mfrow=c(2,1), mar=c(0.1,2,0.1,2))
# Plot the figures
barplot(a)
barplot(b)
The resulting figure should roughly resemble this:
Here is ggplot version using facet_grid:
df <- data.frame(a=runif(3e3), b=rep(letters[1:3], 1e3), c=rep(1:1e3, 3))
ggplot(df, aes(y=a, x=c)) + geom_bar(stat="identity") + facet_grid(b ~ .)

Horizontal scale bar in R plot

When I plot my fitted models in R, I get the output with a vertical scale bar. Can some one kindly tell me how to place a horizontal scale bar in R plots instead of vertical? Thanks in advance.
Not sure what you want to do.
Here are some possible solutions:
1) If using `library(ggplot2)
add + coord_flip()
2) If using plot
plot(x = value, y = 1:length(name),...)

Resources