I have code below. Why don't the text labels appear? I want them on top of the each bar.
x=c(20,30,70,5,10,20)
aaa=plot(table(x),yaxt='n',main="county", ylab="drop offs")
text(aaa,labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
Your text statement only gives the argument aaa to position the text labels. So text is plotting your points at (1,5), (2,10), (3,20), (4,30) and (5,70) - all of which are off the graph and so do not display. You can get almost what you want by changing your text statement to:
text(aaa, table(x), labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
But that cuts off the highest label a little, so I recommend adjusting ylim on the plot.
aaa=plot(table(x),yaxt='n',main="county", ylab="drop offs",
ylim=c(0,max(table(x)))+0.1)
text(aaa,table(x), labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
Related
I have a ggplot in R that displays three labels on the x-axis. By default, these labels are centered. However, I am searching for a way to left-align the first label, center the second, and right-align the third label. The linked photo shows the exact result that I try to replicate. Any suggestions on how I can do this in R?
The label alignment I try to replicate.
qplot(100*1:3,1) + scale_x_continuous(breaks=100*c(1,2,3)) +
theme(axis.text.x = element_text(hjust=c(0,0.5,1)))
I would like to add some totals on the right side of the plot (above and under the legend). And also under the title of the x axis.
I found how to add text within the plot, or add multiple legends of something ploted, but I don't want to do either one. I just want to calculate some totals and display them as mentioned above. Is it in any way possible?
As it is now:
As I would like it to be:
Can I create a plot legend which doesn't have plotting symbols and that the text itself is fitted to the border box?
For example, in the following code there is a gap before the legend text that I want to get rid of:
plot(c(1,2,3),c(1,1,1))
abline(v=c(1.5,2,2.5),col=c("blue","red","green"))
legend("topright",legend=c("legend 1","legend 2","legend 3"),text.col=c("blue","red","green"))
Something you can try :
# draw your legend without the border and with the text left-aligned and save the components :
myleg<-legend("topright",legend=c("legend 1","legend 2","leg 3"),text.col=c("blue","red","green"),plot=T,bty="n")
# get the user coordinates to adjust the gap between the text and the border
coord<-par("usr")
# add a border, closer to the text (here, gap between border and beginning of text is a hundredth of the plot width) :
rect(myleg$text$x[1]-diff(coord[1:2])/100,myleg$rect$top-myleg$rect$h,myleg$rect$left+myleg$rect$w,myleg$rect$top)
If you want to move the text further towards the middle of the plot, you can use inset parameter (eg: inset=0.05) :
myleg<-legend("topright",legend=c("legend 1","legend 2","leg 3"),text.col=c("blue","red","green"),plot=T,inset=0.05,bty="n")
coord<-par("usr")
rect(myleg$text$x[1]-diff(coord[1:2])/100,myleg$rect$top-myleg$rect$h,myleg$rect$left+myleg$rect$w,myleg$rect$top)
I have a plot with no x-label. Instead, I want to just have two text boxes on the bottom left (saying "Negative) and bottom right (saying "Positive").
I have my plot object (p), but have tried different ways to achieve what I want, failing each time. For instance, this does not create the text box in the bottom left of the plot.
p + legend.title("Negative") + legend.position(c("bottom","left"))
Any advice is appreciated!
I think you are looking for the annotate function with which you can add text to a plot:
p + annotate("text", x = x-position-value, y = y-position-value, label = "Negative")
You can also add rectangles, lines and pointranges with this function. For some further details about how to use this function, see the official documentation
The legend that R creates when you call legend() has the symbols (or line types etc) on the left and the labels on the right. I'd like it the other way round, i.e. labels on the left (right-aligned) and the symbols on the right.
I know that I can use adj to adjust the position of the labels, but with this they are not aligned properly anymore. If I set adj=2 for example, the labels are to the left of the symbols, but the end of the text is not aligned with the symbols.
Any pointers on how to do this using either the standard legend() function or a package would be appreciated.
If you set trace = TRUE and then save the output, you can draw the legend and then add the labels with a call to text() using the coordinates given by trace, setting pos = 2 for right alignment. Here's an example:
set.seed(1)
plot(1:10,runif(min=0,max=10,10),type='l',ylim=c(0,10),xlim=c(0,10),col=1)
lines(1:10,runif(min=0,max=10,10),col=2,lty=2)
lines(1:10,runif(min=0,max=10,10),col=3,lty=2)
a <- legend(1,10,lty=1:3,col=1:3,legend=c("","",""),bty="n",trace=TRUE)
text(a$text$x-1,a$text$y,c("line 1","line 2","line 3"),pos=2)