Add values and superscript to pie-labels - r

I'm struggling with adding a superscript to the labels of a plot.
I would like to have the '3' in the labels (..m^3) as superscript. I tried expression(), substitute() etc. but didn't find the correct solution.
values <- c(2, 4, 5)
pie(values, labels = paste(values, "m^3") )
Thanks for any hint!

A bit cumbersome workaround:
foo <- sapply(as.list(values), function(x) bquote(.(x) ~ m^3))
pie(values, labels = as.expression(foo))

Related

R multi boxplot in one graph with value (quantile)

How to create multiple boxplot with value shown in R ?
Now I'm using this code
boxplot(Data_frame[ ,2] ~ Data_frame[ ,3], )
I tried to use this
boxplot(Data_frame[ ,2] ~ Data_frame[ ,3], )
text(y=fivenum(Data_frame$x), labels =fivenum(Data_frame$x), x=1.25)
But only first boxplot have value. How to show value in all boxplot in one graph.
Thank you so much!
As far as I understand your question (it is not clear how the fivenum summary should be displayed) here is one solution. It presents the summary using the top axis.
x <- data.frame(
Time = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Value = c(5,10,15,20,30,50,70,80,100,5,7,9,11,15,17,19,17,19,100,200,300,400,500,700,1000,200))
boxplot(x$Value ~ x$Time)
fivenums <- aggregate(x$Value, by=list(Time=x$Time), FUN=fivenum)
labels <- apply(fivenums[,-1], 1, function(x) paste(x[-1], collapse = ", "))
axis(3, at=fivenums[,1],labels=labels, las=1, col.axis="red")
Of course you can additionally play with the font size or rotation for this summary. Moreover you can break the line in one place, so the label will have smaller width.
Edit
In order to get what have you posted in the comment below you can add
text(x = 3 + 0.5, y = fivenums[3,-1], labels=fivenums[3,-1])
and you will get
however it won't be readable for other boxplots.

Control the gap of pie labels in R?

t = table(iris$Species)
pie(t, labels=rownames(t))
This draws a simple pie. I want that the labels are a little bit more away from the pie. I checked the par() docu but I think I don't understand it completly and I missed the option for that.
This question is explicite about R's own pie() and not related to any other extern R package.
I don't think you can really do this with the pie function. If you look at View(pie) you'll see that the labels are drawn using the text function. This means that they are not really axis labels, and that par has little effect on them. You could try to do stuff by using the arguments of the text function (i.e. pos = 2, offset = 1) but this will affect all labels in the exact same way and results in warnings. To me it seems that the only way is the stupid way by adding some spaces before/ after labels. ie:
t = table(iris$Species)
nms = rownames(t)
# spaces needed after the labels
nms[2] = paste0(nms[2], strrep(' ', 7))
# spaces needed before the labels
nms[c(1, 3)] = paste0(strrep(' ', 7), nms[c(1, 3)])
pie(t, labels = nms)
If you want to a better solution, you could rewrite the pie function to be a bit more flexible or use a different package.

superpose a histogram and an xyplot

I'd like to superpose a histogram and an xyplot representing the cumulative distribution function using r's lattice package.
I've tried to accomplish this with custom panel functions, but can't seem to get it right--I'm getting hung up on one plot being univariate and one being bivariate I think.
Here's an example with the two plots I want stacked vertically:
set.seed(1)
x <- rnorm(100, 0, 1)
discrete.cdf <- function(x, decreasing=FALSE){
x <- x[order(x,decreasing=FALSE)]
result <- data.frame(rank=1:length(x),x=x)
result$cdf <- result$rank/nrow(result)
return(result)
}
my.df <- discrete.cdf(x)
chart.hist <- histogram(~x, data=my.df, xlab="")
chart.cdf <- xyplot(100*cdf~x, data=my.df, type="s",
ylab="Cumulative Percent of Total")
graphics.off()
trellis.device(width = 6, height = 8)
print(chart.hist, split = c(1,1,1,2), more = TRUE)
print(chart.cdf, split = c(1,2,1,2))
I'd like these superposed in the same frame, rather than stacked.
The following code doesn't work, nor do any of the simple variations of it that I have tried:
xyplot(cdf~x,data=cdf,
panel=function(...){
panel.xyplot(...)
panel.histogram(~x)
})
You were on the right track with your custom panel function. The trick is passing the correct arguments to the panel.- functions. For panel.histogram, this means not passing a formula and supplying an appropriate value to the breaks argument:
EDIT Proper percent values on y-axis and type of plots
xyplot(100*cdf~x,data=my.df,
panel=function(...){
panel.histogram(..., breaks = do.breaks(range(x), nint = 8),
type = "percent")
panel.xyplot(..., type = "s")
})
This answer is just a placeholder until a better answer comes.
The hist() function from the graphics package has an option called add. The following does what you want in the "classical" way:
plot( my.df$x, my.df$cdf * 100, type= "l" )
hist( my.df$x, add= T )

Generate a list of expression literals from an integer sequence

I would like to map a sequence of integers to a sequence of expression literals in order to use the latter as tick mark labels in a plot, e.g.
lbls <- lapply(-2:2, function(i) expression(i * pi))
plot(...)
axis(1, at=seq(-2,2)*pi, labels=lbls)
So far I've tried all variations of bquote, substitute, expression etc. that I could think of, but apparently I must have missed something.
Also, the FAQ and related SO questions & answers didn't fully solve this for me.
How would I do it correctly (I want axis to render pi as the greek letter and have -2 ... 2 substituted for i in the above example)?
try this:
lbls <- do.call("expression", lapply(-2:2, function(i) substitute(X * pi, list(X = i))))
plot(-10:10, -10:10, xaxt="n")
axis(1, at=seq(-2,2)*pi, labels=lbls)
Try this:
lbls <- parse(text = paste(seq(-2, 2), "pi", sep = "*"))

use of mathematical annotation as factor in R

I refer to my previous question, and want to know more about characteristics of factor in R.
Let say I have a dataset like this:
temp <- data.frame(x=letters[1:5],
y=1:5)
plot(temp)
I can change the label of x easily to another character:
levels(temp[,"x"]) <- letters[6:10]
But if I want to change it into some expression
levels(temp[,"x"]) <- c(expression(x>=1),
expression(x>=2),
expression(x>=3),
expression(x>=4),
expression(x>=5))
The >= sign will not change accordingly in the plot. And I found that class(levels(temp[,"x"])) is character, but expression(x>=1) is not.
If I want to add some mathematical annotation as factor, what can I do?
I do not see any levels arguments in ggplot and assigning levels to a character vector should not work. If you are trying to assign expression vectors you should just use one expression call and separate the arguments by commas and you should use the labels argument in a scale function:
p <- qplot(1:10, 10:1)+ scale_y_continuous( breaks= 1:10,
labels=expression( x>= 1, x>=2, x>=3, x>= 4,x>=5,
x>= 6, x>=7, x>= 8,x>=9, x>= 10) )
p
I would just leave them as character strings
levels(temp[,"x"]) <- paste("x>=", 1:5, sep="")
If you then want to include them as axis labels, you could do something like the following to convert them to expressions:
lev.as.expr <- parse(text=levels(temp[,"x"]))
For your plot, you could then do:
plot(temp, xaxt="n")
axis(side=1, at=1:5, labels=lev.as.expr)
Expression is used to generate text for plots and output but can't be the variable names per se. You'd have to use the axis() command to generate your own labels. Because it can evaluate expressions you could try...
plot(temp, xaxt = 'n')
s <- paste('x>', 1:5, sep = '=')
axis(1, 1:5, parse(text = s))

Resources