I have downloaded the directlabels package in R to further enhance the ggplot2 experience, however I would like to delete the data point of a scatterplot once I have added the labels to them. Is there any way to hide these? My code goes something like this:
q<-qplot(x,y)+geom_point(aes(colour=z))
direct.label(q,list(cex=0.75,fontface="bold",bumpup))
But I'm not sure where the command to hide the data point would be. I would use first.qp but in this case I get the error
Error in order.labels(d) : labels are not aligned
so is there a better way of doing this?
You example is not reproducible. So I will just answer this question:
"I would like to delete the data point of a scatterplot once I
have added the labels to them."
You can easily for example remove a layer from the gg object. First I create a ggplot2 example and I decorate it using direct.label.
library(directlabels)
scatter <- qplot(jitter(hwy),jitter(cty),data=mpg,colour=class,
main="Fuel efficiency depends on car size")
scatter <- direct.label(scatter,list(cex=0.7,bumpup))
I remaove the first layer now(the geom_point layer)
scatter$layers[1] <- NULL
Then you get this plot , as you see I had only labels without points:
scatter
Related
I am working on a diametric class distribution data, with over 10,000 individuals, and I wanted to make a histogram to show the results, but I am having a problem when I try to plot it.
When I tried using the histogram function
histogram(data)
I get a histogram that is calculating the percentage of individuals
Histogram using the histogram function
Then I tried using qplot function
qplot(data, geom = "histogram")
And got a histogram like the one you can see in image 2
qplot function
So I thought maybe is a problem with the y-axis scale so I tried using ylim() and the plot I got was only the axis with no data
Could someone please tell me what I'm doing wrong?
I'm trying to get a histogram like the one in image 3Histogram goal
I really appreciate your help to point out if I am not adding other codes or maybe redirect me to a cheat sheet that can clarify some things
thanks
Try to use the ggplot2 package, for example:
library(ggplot2)
ggplot(data = [YOUR_DATA], aes([YOUR_VARIABLE])) +
geom_histogram()
EDIT:
Looking at your desired result, I think you want to use barplot() instead of histogram().
I’m using the metafor package to create Forest plots in R. I’d like to add text to my plots to create labels using the text() function. I’m wondering what the simplest way is to determine the x,y coordinates of where I want my text to go. Currently I just guess and see how it looks and then edit as necessary. Is there a way to overlay a grid over my plot or something to guide me (and then remove it after)?
Thank you!
Start by saving what forest() returns:
x <- forest(res)
And then take a look at x. Among other things, it contains xlim and ylim, which are the x-axis and y-axis limits. Try this:
abline(v=x$xlim)
abline(h=x$ylim)
Also useful:
text(x$xlim[1], x$rows, x$rows, xpd=NA)
I hope this helps.
I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)
##Example data to illustrate problem:
type=c("grp1","grp2","grp1","grp3","grp3","grp3","grp4")
num=c(1,1,2,4,3,5,1)
cols=c(rep("red",5),"green","red")
library(lattice)
bwplot(num~type)
par(new=T)
stripplot(num~type,col=cols)
I like the additional information displayed by the box plot but I need the information conveyed by the coloured points in the strip chart. Obviously par(new=T) doesn't work, so how can I overlay the points onto the box plot?
You can define a panel function like this :
library(lattice)
bwplot(num~type,panel=function(x,y,...){
panel.bwplot(x,y,...)
panel.stripplot(x,y,col=cols,...)
})
the answer by agstudy is quite good, though as I pointed out in a comment, any outliers plotted by bwplot will also be plotted by stripplot, which could get confusing.
Note this is only an issue if you were using jitter.data=TRUE as an option to stripplot, otherwise the duplicate points would plot directly over top of one another and you'd never know they were there. If the data had outliers, and you used jitter.data=TRUE, you'd see twice as many outliers as you should.
the following approach suppresses the outliers from bwplot to avoid this issue:
bwstrip <- function(x,y,...){
panel.stripplot(x,y,col=cols,do.out=FALSE,jitter.data=TRUE,...)
panel.bwplot(x,y,...)
}
bwplot(lobe.depths,panel=bwstrip)
I just wanted how know what category matches with everypoint when I do this:
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group)
I know how to make a legend and put text with an arbitrary vector c('A','B',C'), but is there a more "automatic" way for doing this? This is an easy example but I need to do it with residuals or survival functions plot
Thank you in advance.
The traditional graphics system provides the legend function for adding a
legend or key to a plot. But It should be noted that it is entirely the responsibility of the user to ensure that the legend corresponds to the plot. There is no automatic checking that
data symbols in the legend match those in the plot. It is simpler to do it using lattice or ggplot2. for example:
library(lattice)
xyplot(y~x,groups=group,auto.key=T)
if you want absolutly to use base graphics, you can do this :
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group,pch=as.numeric(group))
legend(2, 500, c('A','B','C'),
cex=1.5, pch=1:3,col=1:3)