I'd like to plot a (kind of) barchart, but rather than using bars, I'd like to use a unicode symbol. Is this possible?
I know how to use a unicode character as a symbol (points); for example:
plot(1,1,pch=-0x0001F3E2, cex=5, col="gray30")
but how can make the symbol substitute the bars?
EDIT:
As suggested by Eric, I'll try to write a minimal reproducible example...
Given variable x:
x <- c(2,3,4,2,1)
barplot(x)
I want to produce a barplot using custom symbols rather than bars. Something like:
plot(1:5,rep(0,5),pch=-0x0001F3E2, cex=x, col="gray30")
The problem is that I am not able to vertical align sysmbols as I can do with text:
plot(1:5,rep(0,5),type="n", ylim=c(0,1),bty="n")
text(1:5,0,"A",cex=c(2,5,3,3,1),adj=c(0.6,0))
So, a sort of solution to my problem, although not ideal, is to use
some custom system font with package extrafont.
For example, using font "Destiny's Little Houses" (http://www.dafont.com/destiny-little-hous.font)
I am able to produce some nice infographics:
plot(1:5,rep(0,5),type="n",ylim=c(0,1),bty="n")
text(1:5,0.1,"A",cex=c(2,5,3,3,1),adj=c(0.6,0),family="Destiny's Little Houses")
So, rather than having bars I have an image whose size changes according to some variable (using "cex").
This is not ideal, first of all because I'd like to be able to import any image (ex. using the png package).
So, is it possible to:
import an image file and use it as the symbol in plot (rather than one of the available sysmbols using pch)?
Vertical align symbols so that I get a kind of custom barchart?
Thanks
António
Here's a somewhat hackish start (might not be helpful as I don't know what your data looks like).
counts <- table(mtcars$gear)
foo <- barplot(counts, col = c("white", "white", "white"), border=c("white"), axes=F, xaxt="n")
points(foo, counts-.5, pch = -0x0001F3E2, cex=2, pos=3)
Remove , axes=F, xaxt="n" to get the labels and axis back.
Related
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 use the plotROC from hmeasure-package to (try to) plot an ROC curve. I get a plot like shown below. The code I used is:
require(hmeasure)
predictions_LIMO <- data.frame(df)
h_LIMO <- HMeasure(mydata$churn, predictions_LIMO)
plotROC(h_LIMO,which=1)
If I try to customize the main title, I get the two titles overlapping each other, like shown in the lower picture. The additional code is:
title("ROC curve for LIMO4 to LIMO8")
Is there a way to get rid of the automatically assigned title? I tried main=0 but received unused argument (main = 0).
In the ?plotROC I did not find anything about that.
Thanks for any advice (maybe also on different packages that I can use to circumvent the problem)!
As #joran said, it's hard-coded. Here's a quick hack if you don't want to mess with the plotROC function. You can "cover" the existing title with a white rectangle and then add your own title. For example:
# Adjust location of rectangle as needed. These coordinates should work
# for the graph you posted.
rect(0, 1.1, 1, 1.7, xpd=TRUE, col="white", border="white")
title("My Title")
A beginner question: I currently have some plots that look like this.
I'm keeping the plots free of other annotation because I have to fit a lot of them onto one page and specific values aren't important, just the general trend that the red line is to the right of the black one. However, I'd like to be able to indicate that the dashed line in my plot actually represents zero, like so:
Is there any way to tell R to only display that value on the x-axis labels? I don't think using text() would be a good idea because it relies on absolute coordinates, but I'm not really sure how to work with axis options to make it do what I want.
Try:
axis(side = 1, at = 0)
See ?axis for details.
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)
I am doing a barplot of 14 columns to represent some data, I set the names.arg option to show as vertical lables, unfortunately, this caused the new vertical lables to overlap with the "sub" and "xlab" options I have. How do I prevent this from happening?
Here's my command:
par(mar=c(6, 5, 4,7.5 ))
barplot(x, main=paste("title1 \n","subtitle"),
names.arg=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14),las=2, sub=("overlapping text1"),
xlab="overlapping text2", col = c("red2","darkorange3"))
Another question on my mind is, I am using a 2-line title in "main" as you can see from the command. Is it possible to make the second line font smaller, whilst keeping the first line in the same format?
Thanks,
One solution to change font size for one of titles, is to use two calls of functions mtext() in different lines with different cex= values and remove main= from barplot(). To overcome problem with overlapping text, mtext() also can be used instead of xlab= and sub=. You just have to find the right line= and increase space around plot with par(mar=..).
x<-sample(letters[1:14],300,replace=TRUE)
par(mar=c(9,3,5,2))
barplot(table(x),
names.arg=paste0("very_long_",1:14),las=2,
col = c("red2","darkorange3"))
mtext(side=3,"Title1",line=2,cex=2)
mtext(side=3,"subtitle",line=1,cex=1.5)
mtext(side=1,"overlapping text1",line=6)
mtext(side=1,"overlapping text2",line=7)
Another option to look at is the staxlab function in the plotrix package.
Also look at the mgp argument to the par function for a way to set the default placement of the axis title.