I use geom_text to plot some text. A text example would be:
a -> 7.0
However, instead of using "->" as an arrowhead I would like to use an arrowhead symbole.
I found the package plotmath to be used with ggplot to plot symboles.
I am not sure if it wouldn't be easier to plot the text separated from the actual symbol and align them using different coordinates that are close together so that once plotted it looks like a single string.
You'll want to pass it as an expression and use the parse=TRUE option for geom_text. The geom_text documentation should provide you with enough to get going:
Plotmath with geom_text: link.
Related
I want to add an additional tick to my plot in ggplot2, and used Solution 2 as described in the post Annotate ggplot with an extra tick and label.
It worked fine for me, giving the following result in R Studio:
But when I try to save the result using ggsave() to create the a .pdf, .ps, or .png file, the red number is cut off half like this:
I have the feeling that the inner plot is printed first and later the margins are plotted on top of this.
Anybody has a hint?
Thank you Z. Lin! I just had a grid.draw(g) instead of g <- grid.draw(g). This dot in R always activates my python brain region :)
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.
I've been working on this for days and can't seems to find a reproducible code (or even a lead) on how to do this. Long story short, I'm trying to create a publication quality figure in which I want my facet labels to look like this (although the -1 should be superscript :/):
(A) Decomposition rate (g·d^-1)
So I want my facet labels to both have a bold text, plain text, and math component. I'm using facet_wrap instead of facet_grid because I want to be able to specify the number of columns/rows. While I know how to make axis labels and such like this using
lab=expression(paste(bold("(a)")~plain("Decomposition rate")~"(g"%.%d^-1")"))
I can't just change the variable levels/names in the data frame b/c then ggplot2 doesn't recognize the bold and math expressions. Any leads or ideas would be much appreciated!
I have a data set, and one of the variables is a factored array with hexadecimal characters (e.g. '#00FF00'). One of the things I wanted to try doing is creating a bar plot with all of the different colors combined.
I tried using
cg<-ggplot(my.data,aes(x=factor(1),fill=as.character(my.color)))
followed by
cg+geom_bar()
but the only colors plotted seem to be ones from the default scale. I've tried omitting the as.character() part of the code, but it doesn't make a difference. I also have the same issue when making 2d plots with geom_point().
If I try something like
plot(my.data$var1,my.data$var2,col=as.character(my.color))
the colors are plotted the way I wanted them, although the graph doesn't look as nice as the ones in ggplot2.
Is there something obvious I'm missing, or is this beyond the scope of ggplot2?
You should add scale_fill_identity() to use color names as actual colors.
ggplot(my.data,aes(x=factor(1),fill=my.color)) +
geom_bar()+
scale_fill_identity()
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)