I have a graphing question that I feel I should know the answer to, but I can't quite figure it out. I've specified 2 different fill colors & have made a legend of it, but when I generate the graph, the fill colors are either...
way too bright on the graph, but just the right in the legend
just right on the graph, but not even visible in the legend
It looks like this has to do with the 'alpha' value I specify, but I'm not sure how to fix this. Any help is appreciated!
#My graphing code
#graph the average output by machine
ggplot(data=tempdf1)+
#dot plot of individual POD machines
geom_point(aes(x=sample,y=mean_perc_FAM_to_ref,color=POD_machine),width=0.1)+
#line plot of the mean across POD machines
stat_summary(aes(x=sample,y=mean_perc_FAM_to_ref,group=known_conc,color=POD_machine),fun=mean,geom="line")+
#wrap by known conc
facet_wrap(.~known_conc,scales="free")+
#ref box for 10% variation from known trait conc
geom_rect(aes(xmin=p10xmin-1,xmax=p10xmax+1,ymin=p10ymin,ymax=p10ymax,fill="10% Error"),alpha=0.005)+
#ref box for 5% variation from known trait conc
geom_rect(aes(xmin=p5xmin-1,xmax=p5xmax+1,ymin=p5ymin,ymax=p5ymax,fill="5% Error"),alpha=0.005)+
scale_fill_manual(values=c("5% Error"="green","10% Error"="yellow"))+
#labels
labs(x="Sample",y="Measured % of Trait",color="POD Machine",fill="Error Range")+
#clean up background, add gridlines (only to y-axis), add x- and y-axis lines
theme(panel.background=element_blank())+
theme(panel.grid=element_line(color="black"))+
theme(panel.grid.major=element_blank())+
theme(panel.grid.minor=element_blank())+
theme(axis.line=element_line(color="black"))+
#customize x-axis so there is no space between graph and y-axis
scale_x_continuous(expand=c(0,0))+
#customize tick marks in y-axis
scale_y_continuous(labels=scales::percent)
When I specify "alpha=0.005" for geom_rect, this is the graph I get
When I specify "alpha=0.5" for geom_rect, this is the graph I get
Thanks for your help!
Related
I am drawing a PC plot using ggplots.
I know this question has been answered in some previous posts but I could not still solve my problem.
I have a data set called tab which is the output of PCA
sample.id pop EV1 EV2
HT185_MK8-2.sort.bam HA_27 -0.03796869 0.046369552
HT48_SD1A-37.sort.bam HA_14 0.04208393 0.032961404
HT53_IA1A-10.sort.bam HA_1 -0.02580365 0.005262476
HT260_MK1-4.sort.bam HA_20 -0.06090545 0.005578504
HT170_SD2W-14.sort.bam HA_17 0.01288395 0.012117833
Q093_MK7-13.sort.bam HA_26 0.06310162 0.188558067
I want to add labels on each dot in the plot, theses dots are individuals from several populations. So I want to give them their population ID (pop column in the data set).
I am using something this
ggplot(data=tab,aes(EV1,EV2, label=tab[,2])) + geom_point(aes(color=as.factor(pop))) + ylab("Principal component 2") + xlab("Principal component 1")
But I do not get my desired output.
This is my PC plot!
So could anyone help me to add population label on each dot in the plot!
Thanks
Try geom_text:
geom_text(aes(label=as.character(pop)),hjust=0,vjust=0)
Also consider looking into plotly, or setting a threshold on the labels, because labeling every point will lead to a very crowded plot, and probably very little additional useful information.
I am using ggplot2 to make several area plots of time series. To my eye, the plots look better if the time series covers the entire x axis, the height of the highest area is about 5% - 10% below the top of the plot area, and the legend is situated in the lower right corner of the plot.
Let base.plot be a base plot that labels the x axis and formats its tick marks, adds NBER recession bars, and locates the legend in the lower right corner of the plot itself with:
base.plot <- base.plot + theme(
legend.justification = c(1,0),
legend.position = c(1,0),
legend.title = element_blank()
)
This seems to work fine with my line plots, but on the area plots the legend box sticks out to the right and below the plot itself. Instead, its lower right corner should be at the lower right corner of the plot area. How can I fix this?
To change the plot's extent relative to its axes, I tried using the expand argument to expand the plot horizontally and vertically. Documentation for this argument leaves something to be desired, to say the least:
expand
A numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes. The defaults are c(0.05, 0) for continuous variables, and c(0, 0.6) for discrete variables.
Is it too much to ask for the formula so we can know what the multiplicative and additive constants actually do? Otherwise, how else can we know how to set them? The above description appears in the documentation for scale_x_date; is it too much to ask for some mention of the defaults for date variables?
Flying blind, thanks to the useless documentation, I tried the solution for continuous variables:
scale_x_date(expand = c(0,0)),
But this just scrunched up the plot towards the right of the chart. So where can I learn about using scale_x_date with the expand argument?
As for the vertical axis, scale_y_date(expand = c(0,0)) did bring the bottom of the area plots down to the x-axis. But the top is too high. Somewhere I saw that a modification to the scale_y_date code now allows four arguments, two for the lower bound and two for the upper one. I tried this too, but there's no discernible difference from the plot using only the two parameters.
So, how can I get the lowest area plot to sit on the x axis and the highest point to be about 0.5 in from the top?
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
Say I have some data on some experiment I conducted on Earth and on Wayne's World. There are control and treatment means:
means1<-c(1,2)
means2<-c(1.5,2.5)
data<-cbind(means1,means2)
rownames(data)=c('ctrl','treatment')
colnames(data)=c('Earth','Waynes World')
I would like to plot this data, so I do.
barplot(data,beside=T)
This generates paired control and treatment bars, separated by planet. Each pair of bars has an x axis label specifying what planet they are from. What I would like is a second set of x-axis labels underneath each bar that specifies ctrl or treatment. Bonus if you tilt this second set of labels, they don't overlap the first labels, and everything looks pretty.
I think something like this describes what you're after
bp<-barplot(data,beside=T, xaxt="n")
mtext(text=rownames(data)[row(bp)], at=bp, line=1, side=1)
mtext(text=colnames(data), at=colMeans(bp), line=2.2, side=1)
Hi I got a data frame weekly.mean.values with the following structure:
week:mean:ci.lower:ci.upper
Where week is a factor; mean, ci.lower and ci.upper are numeric. For each week, there is only one mean, and one ci.lower or ci.upper.
I was trying to plot a shaded area inside of the 95% confidence interval around the mean, with the following code:
ggplot(weekly.mean.values,aes(x=week,y=mean)) +
geom_line() +
geom_ribbon(aes(ymin=ci.lower,ymax=ci.upper))
The plot, however, came out blank (that is only with x-axis and y-axis present, but no lines, or points, let alone shaded areas).
If I removed the geom_ribbon part, I did get a line. I know that this should be a very simple task but I don't know why I couldn't get geom_ribbon to plot what I wanted. Any hint would be truly appreciated.
I realize this thread is super old, but google still find it.
The answer is that you need to set the ymin and ymax to use a part of the data you are using on the y-axis. It you set them to scalar values then the ribbon covers the entire plot from top to bottom.
You can use
ymin=0
ymax=mean
to go from 0 to your y-point or even
ymin=mean-1
ymax=mean+1
to have the ribbon cover a strip encompassing your actual data.
I may be missing something, but the ribbon will be plotted filled with grey20 by default. You are plotting this layer on top of the data so no wonder it obscures it. Also, it is also possible that the limits for the plot axes derived from the data provided to the initial ggplot() call will not be sufficient to contain the confidence interval ribbon. In that case, I would not be surprised to see a grey/blank plot.
To see if this is the problem, try altering your geom_ribbon() line to:
geom_ribbon(aes(ymin=ci.lower,ymax=ci.upper), alpha = 0.5)
which will plot the ribbon with transparency whic should show the data underneath if the problem is what I think it is.
If so, set the x and y limits to the range of the data +/- the confidence interval you wish to plot and swap the order of the layers (i.e. draw the line on top of the ribbon), and use transparency in the ribbon to show the grid through it.
From ggplot's docs for geom_ribbon (2.1.0):
For each continuous x value, geom_interval displays a y interval. geom_area is a special case of geom_ribbon, where the minimum of the range is fixed to 0.
In this case, x values cannot be factors for geom_ribbon. One solution would be to convert week from a factor to a numeric. e.g.
ggplot(weekly.mean.values,aes(x=as.numeric(week),y=mean)) +
geom_line() +
geom_ribbon(aes(ymin=ci.lower,ymax=ci.upper))
geom_line should handle the switch from factor to numeric without incident, although the X axis scale may display differently.