I'm trying to use UTF-8 code to put a symbol in some text in an R figure. If I wanted a black circle, I could use the code intToUtf8(9679). Where can I find a database that lists the values for other symbols? I want to find the code to create a red circle (i.e., pch=16, col="red"), but I can't find a list of what all of the unicode values are for specific symbols.
# example R code
x <- 1:10
y1 <- rnorm(10, x*2, 0.5)
y2 <- rnorm(10, x, 0.5)
plot(x, y1, xlab='x-value', ylab='', pch=16)
points(x, y2, pch=16, col='red')
mtext(paste0('value for y1 (', intToUtf8(9679), ') and y2 (', intToUtf8(9679), ')'), side=2, line=2)
# except that I want the second black circle in the axis label to be a red circle
Thank you for your help,
Mikey
Here is the solution I ended up using. It's not the most efficient, but it works:
x <- 1:10
y1 <- rnorm(10, x*2, 0.5)
y2 <- rnorm(10, x, 0.5)
plot(x, y1, xlab='x-value', ylab='', pch=16)
points(x, y2, pch=16, col='red')
mtext('value for y1 (\u25CF) and y2 ( )', side=2, line=2)
mtext(' \u25CF', side=2, line=2, col='red')
# alternatively, the following would also work in place of line 6
mtext(paste0('value for y1 (', intToUtf8(9679),' and y2 ( )'), side=2, line=2)
If you want to find more unicode character information for your specific symbol, you can look here.
Thank you lukeA for your help with this.
Related
I am trying to make a layer diagram in base R with three layers:
the area under the y1 line shaded cyan,
the area between the y1 and y2 line shaded yellow and
the area between y2 and y3 shaded grey.
I tried:
x <- 1:100
y1 <- 2*x+3
y2 <- y1 +4*x+9
y3 <- y2+ 1.5*x
plot(x,y3,type="l")
polygon(x,y3,col="grey",border="grey")
polygon(x,y2,col="yellow",border="yellow")
polygon(x,y1,col="cyan",border="cyan")
but this did not work.
Thank you for your help.
You were close, the polygon needs to return to the x axis, thus we need to add coordinate [100, 0]. I show border=F, but maybe yours is better. You probably should plot lines at the end if wanted.
plot(x, y3, type="n")
polygon(c(x, 100), c(y3, 0), col="grey", border=F)
polygon(c(x, 100), c(y2, 0), col="yellow", border=F)
polygon(c(x, 100), c(y1, 0), col="cyan", border=F)
lines(x, y3, lwd=2, col="red")
Within one Figure, I have used the first default linetypes (lty) except the blank one ("0"), and from ggplot2 for the seventh lt="F2" with the lines-function
lines( ..., lty=5, lwd=2)
lines( ..., lty=6, lwd=2)
lines( ..., lt="F2", lwd=2)
--> which works fine when displaying the different lines within one Figure.
However, I also want to denote them together in one legend with (I also tried several similar approaches):
legend(x=c( ..., ...),
y=c( ..., ...),
lty=1:6, lt="F2" < here I want to have linetype "F2" listed at the seventh position, right after lty=6>,
lwd=2
legend=c(expression(paste("some text 1")),
...
expression(paste("some text for 6")),
expression(paste("some text for 7"))))
I altered the example to a minimal reproducible example in R:
# In the legend, I would like to replace the "3" in lty=c(1, 2, 3) by lt="F2"
x <- seq(-3.5,3.5,0.01)
y1 <- x^2
y2 <- x^2+1
y3 <- x^2+2
plot(x, y1, type="l", lwd=2,
xlab="x label",
ylab="y label")
lines(x, y2, lty=2, lwd=2)
lines(x, y3, lt="F2", lwd=2) # different linetype "F2", works fine when displaying
legend(x=c(2, 3.5),
y=c(0, 3.8),
lty=c(1, 2, 3), lwd=2, # instead of lty="3" I would like to use "F2" in the legend
legend=c(expression(paste("y1")),
expression(paste("y2")),
expression(paste("y3"))))
Do you have any ideas to also list the seventh linetype of ggplot2 after the ones of the R default linetypes?
Thanks in advance!
I've created secondary Y axis, and I've put a label there as well using mtext. However, I can't figure out how to rotate my secondary Y label in the way to face a plot - like my red Y2 label ?
My dummy data, adopted from : http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red")
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("y2",side=4,line=3)
legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2"))
result:
I've tried srt = ..., of las = ..., neither of them does work.
I don't necessary need to use mtext, is there please another simple solution ?
Thank you !
Use text instead of mtext:
set.seed(1)
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red")
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
text(par("usr")[2]*1.11,mean(par("usr")[3:4]), "y2", srt = -90, xpd = TRUE, pos = 4)
legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2"))
(via)
To plot the empirical cumulative density of three variables, x1, x2 and x3, I used the following in r:
plot.ecdf(x1, col="blue",
main="Distribution XYZ",
xlab="x_i", ylab="Prob(x_i<=y)")
lines(ecdf(x2), col="red") # adds a line
lines(ecdf(x3), col="green") # adds line
legend(600,0.6, # places a legend from (x,y)=(600,0.6) on
c("x1","x2","x3"), # puts text in the legend
lty=c(1,1,1), # gives the legend appropriate symbols (lines)
lwd=c(1,1,1),col=c("blue","red","green")) # gives the legend lines the correct color and width
The resulting plot however has two horizontal lines (broken lines) at 0 and 1 besides the box. And, the origin of the box has a space below zero on the vertical axis and a space to the left of zero on the horizontal axis. May you suggest how to remove these space and the additional lines. I wanted but could not post the plot.
EDITED:
A sample data can be generated as follows:
sample data
n <- 1000; u <- runif(n)
a <- -4.46; b <- 1.6; c <- -4.63
d <- ( a * u ) + (b * ( ( 1.5 * ( u ** 2 )) - 0.5 )) + (c * ( (2.5 * (u ** 3)) - (1.5 * u )))
x1 <- -126/d; x2 <- -131/d; x3 <- -187/d
It sounds like you are asking for the style provided by different settings for 'xaxs' and 'yaxs', and maybe 'xlim':
Try:
plot.ecdf(x1, col="blue",
main="Distribution XYZ",
xlab="x_i", ylab="Prob(x_i<=y)",
xaxs="i",yaxs="i", xlim=c(0,1000) )
lines(ecdf(x2), col="red")
lines(ecdf(x3), col="green")
legend(600,0.6,
c("x1","x2","x3"),
lty=c(1,1,1),
lwd=c(1,1,1),col=c("blue","red","green"))
use the yaxs = "i" argument:
plot.ecdf(x1, col="blue",
main="Distribution XYZ",
xlab="x_i", ylab="Prob(x_i<=y)", yaxs = "i")
This will align the axis at the ylim points (which supposedly are 0 and 1 for plot.ecdf).
If you also want to remove the dotted line at 0 and 1, just call box():
box()
You could use the arguments xlim, ylim and col.01line:
x1 = runif(100)
x2 = runif(100)
x3 = runif(100)
plot.ecdf(x1, col="blue", main="Distribution XYZ",xlab="x_i", ylab="Prob(x_i<=y)", ylim=c(0, 1), xlim=c(0,1), col.01line="white", verticals=FALSE)
lines(ecdf(x2), col="red", col.01line="white")
lines(ecdf(x3), col="green", col.01line="white")
legend(600,0.6,c("x1","x2","x3"), lty=c(1,1,1), lwd=c(1,1,1),col=c("blue","red","green"))
Alternatively, you could just use the generic plot:
f1 = ecdf(x1)
f2 = ecdf(x2)
f3 = ecdf(x3)
pts = seq(0, 1, 0.01)
plot(pts, f1(pts), col="blue", type="b", xlab="x_i", ylab="Prob(x_i<=y)", pch=16, main="Distribution XYZ")
lines(pts, f2(pts), col="red", type="b", pch=16)
lines(pts, f3(pts), col="green", type="b", pch=16)
legend("topleft", c("x1","x2","x3"), fill=c("blue","red","green"))
the type argument to xyplot() can take "s" for "steps." From help(plot):
The two step types differ in their x-y preference: Going from
(x1,y1) to (x2,y2) with x1 < x2, 'type = "s"' moves first
horizontal, then vertical, whereas 'type = "S"' moves the other
way around.
i.e. if you use type="s", the horizontal part of the step has its left end attached to the data point, while type="S" has its right end attached to the data point.
library(lattice)
set.seed(12345)
num.points <- 10
my.df <- data.frame(x=sort(sample(1:100, num.points)),
y=sample(1:40, num.points, replace=TRUE))
xyplot(y~x, data=my.df, type=c("p","s"), col="blue", main='type="s"')
xyplot(y~x, data=my.df, type=c("p","S"), col="red", main='type="S"')
How could one achieve a "step" plot, where the vertical motion happens between data points points, i.e. at x1 + (x2-x1)/2, so that the horizontal part of the step is centered on the data point?
Edited to include some example code. better late than never I suppose.
I am using excellent #nico answer to give its lattice version. Even I am ok with #Dwin because the question don't supply a reproducible example, but customizing lattice panel is sometimes challenging.
The idea is to use panel.segments which is the equivalent of segments of base graphics.
library(lattice)
xyplot(y~x,
panel =function(...){
ll <- list(...)
x <- ll$x
y <- ll$y
x.start <- x - (c(0, diff(x)/2))
x.end <- x + (c(diff(x)/2, 0))
panel.segments(x.start, y, x.end, y, col="orange", lwd=2)
panel.segments(x.end[-length(x.end)], y[1:(length(y)-1)],
x.end[-length(x.end)], y[-1], col="orange", lwd=2)
## this is optional just to compare with type s
panel.xyplot(...,type='s')
## and type S
panel.xyplot(...,type='S')
})
This is a base graphics solution, as I am not too much of an expert in lattice.
Essentially you can use segments to draw first the horizontal, then the vertical steps, passing the shifted coordinates as a vector.
Here is an example:
set.seed(12345)
# Generate some data
num.points <- 10
x <- sort(sample(1:100, num.points))
y <- sample(1:40, num.points, replace=T)
# Plot the data with style = "s" and "S"
par(mfrow=c(1,3))
plot(x, y, "s", col="red", lwd=2, las=1,
main="Style: 's'", xlim=c(0, 100))
points(x, y, pch=19, col="red", cex=0.8)
plot(x, y, "S", col="blue", lwd=2, las=1,
main="Style: 'S'", xlim=c(0, 100))
points(x, y, pch=19, col="blue", cex=0.8)
# Now plot our points
plot(x, y, pch=19, col="orange", cex=0.8, las=1,
main="Centered steps", xlim=c(0, 100))
# Calculate the starting and ending points of the
# horizontal segments, by shifting the x coordinates
# by half the difference with the next point
# Note we leave the first and last point as starting and
# ending points
x.start <- x - (c(0, diff(x)/2))
x.end <- x + (c(diff(x)/2, 0))
# Now draw the horizontal segments
segments(x.start, y, x.end, y, col="orange", lwd=2)
# and the vertical ones (no need to draw the last one)
segments(x.end[-length(x.end)], y[1:(length(y)-1)],
x.end[-length(x.end)], y[-1], col="orange", lwd=2)
Here is the result: