Legend formatting in R - what goes wrong? - r

I am trying to get a correct legend for a series of points in a plot, which look like:
plot(c(3,1),pch=21,bg="white",col="black")
points(c(2,1),pch=21,bg="black",col="black")
points(c(1,1),pch=21,bg="dark grey",col="black")
I thought the legend code would be:
legend("topright",legend=c('Point3','Point2','Point1'),pch=c(21,21,21),
bg=c('white','black','dark grey'),col=c('black','black','black'),bty='n')
But apparently I am wrong, because I only get three white points with black boarder. Why isn't this working and what is the correct code?

If you read help("legend") you find out that bg specifies the background of the legend. You need to use pt.bg:
legend("topright", legend=c('Point3','Point2','Point1'), pch=c(21,21,21),
pt.bg=c('white','black','dark grey'), col=c('black','black','black'), bty='n')

Related

Can someone help me with this plot

I have to make this exact plot but I can't seem to find the right color in the hexadecimal chart.
Also does anyone know how to change the labels on the y-axis as well. My values show up as 1e+07 instead of 10,000.
Here is the code I used to make my plot
The data is from the USAarrests data set from R.
p=ggplot(data=examdata,aes(Assault,Population))
p=p+geom_point(size=3,color="red",shape="asterisk")
p=p+geom_smooth(color="Purple",lty="dashed",fill="#71F563")
Here is the picture after I tried the color code you all found.
Edited Plot]2
I think I have to change the alpha on it, but I dont know how to do that with fill. Sorry if these questions have been asked.
I looks like a custom color palette to me. You can use an color picker tool (such as https://imagecolorpicker.com/) to see the exact hex. I tried it out and got this:
Green (CI): #7ef376
Purple (dashed line): #a251ef
Red (data points): #e93f33
Can you also show the code so we know what you did to get your y-axis?

Mixed geom_line & geom_point plot: remove marker from color scale

I often have to use plots mixing lines and points (ggplot2), with the colors of the line representing one variable (here, "Dose"), and the shape of the points another one (here, "Treatment). Figure 1 shows what I typically get:
Figure 1: what I get
I like having different legends for the two variables, but would like to remove the round markers from the color scale, to only show the colors (see legend mockup below, made with Gimp). Doing so would allow me to have a clean legend, with colors and shapes clearly segregated.
Figure 2 (mockup): what I would like
Would anyone know if there is a way to do that? Any help would be much appreciated.
Note: the plots above show means and error bars, but I have the same problem with any plot mixing geom_line and geom_point, even simple ones.
Thanks in advance !

white lines on heat map made by ggplot2

Look at the following picture, I tried to get a heat plot with ggplot2, but there is a white vertical line on it. What happens? Anybody gives me an hint?
Besides, there is a white line on each side of plot, why couldn't the heat plot fill the whole part while the contour fill the whole part?
Lucky! I've sovled the problem with setting an ultra-small left or right margin.

R Make barchart with unicode symbol instead of bars

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.

How to plot just one axis label in R?

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.

Resources