I am trying to rename the group names in a stripchart. I have tried using bquote and expression, but ran into the same problem. Everything within the brackets following expression is printed as a group label without formatting (so in the example below I am getting a label that reads "Delta ~ "Group1"".
I've tried combining expression and paste, but that gave me a label that read "paste(Delta, "Group1")".
stripchart(Df$value~Df$Treatment ,
vertical = TRUE,
group.names = c(expression(Delta ~ "Group1"), "Group2"),
Try setting your group.names=NA and then using the axis command to add the group.names afterwards.
I couldn't reproduce your example but if you do something like:
## define what you want your group.names to be using expression
group_names=c(expression(Delta==1),expression(Delta==2),expression(Delta==3),expression(Delta==4),expression(Delta==5))
## make your strip chart
stripchart(Temp~Month, data=airquality,group.names=NA,vertical=TRUE)
## add the lower axis labels, which equivalently adds the group.names
axis(1,at=1:5,labels=group_names)
Related
Let's say I have
paste0("Year = ",index,"\nN = ",length((dfGBD %>% filter(year==index))[[vbl]]),
" Bandwidth = ",round(stats::bw.nrd(log((dfGBD %>% filter(year == index))[[vbl]])),2),
"\nSkewness:", round(e1071::skewness(log((dfGBD %>% filter(year==index))[[vbl]])), 2),
" Kurtosis:",round(e1071::kurtosis(log((dfGBD %>% filter(year==index))[[vbl]])),2),
"\nmu[",vbl,"] = ", round(mean((dfGBD %>% filter(year==index))[[vbl]]),2),
" sigma[",vbl,"] = ",round(sd((dfGBD %>% filter(year==index))[[vbl]]),2)
)
inside a sapply through index years. Further, vbl is a string with the name of a variable. The sapply produces a vector of labels for a factor variable.
Applying ggplot I obtain labels similar to the next:
Year = 2000
N = 195 Bandwidth = 0.09
Skewness: 0 Kurtosis: -0.56
mu[Mortality] = 7750.85 sigma[Mortality] = 1803.28
Till here, all ok. I have already written mu[vbl], sigma[vbl] thinking in parsing and subscript notation to get the greek letters with the name of the variable saved in vbl as subscript.
First I tried facet_wrap with option labeller = "label_parsed". I obtained an error which I only solved writting the string between backticks ``, but then \n has no effect. I tried many options using bquote and/or parse and/or expression and/or atop etc. in order to get this multiple lines result with the desired output I described above. But only get or one line or very ugly outputs or, mostly, errors, and I couldn't see yet the greek letters.
So what/how should I do?
PS: as answered in other stackoverflow's, \n does not work in this context, so a list with bquote's for each line is suggested. I tried it, but then I got an error that I think is due to incompatibility of number of elements of all the lists and number of labels of a factor (a label may not be a list?).
Thank you!
Within my shiny app, I am having my users select n number of attributes to plot within my splom() call, which essentially follows these steps:
User chooses a number.
User chooses variables.
Code subsets my data set based on users choices.
Splom is called in a very simple call, similar to: lplot <- splom(data_subset).
However, since the panels within splom() are being labeled based on the attribute name (which is reactive, so I can't label ahead of time) and the names are very long you can't read them.
Does anyone know if there is a way to wrap text within the splom panel outputs? The wrapping would be done on the attribute reactive value, so it wouldn't be done on a string.
This is what my output looks like:
After subsetting in step 3, you can manipulate the names of the data subset to wrap appropriately.
## Subset the data in whatever way shiny does (here's a reproducible example)
irisSub <- subset(iris, select = grepl("Width", names(iris)))
Then
Replace "word" separators with spaces. In your example, you probably just need "_"
names(irisSub) <- gsub("\\.|_", " ", names(irisSub))
Wrap these strings at a fairly narrow width. You might have to experiment with other widths here.
wrapList <- strwrap(names(irisSub), width = 10, simplify = FALSE)
Paste these strings back together with a linebreak (\n) in between and assign them to names of data subset
names(irisSub) <- vapply(wrapList, paste, collapse = "\n", FUN.VALUE = character(1))
splom should respect the line breaks
splom(irisSub)
I am using ggpairs and while plotting the matrix, I receive a matrix as follows
As you can see, some of the text length is large and hence the text is not seen completely. Is there anyway that I can wrap the text so that it is visible completely.
Code
ggpairs(df)
I want the text to wrap so that it can be seen something like this
You can use the labeller argument of ggpairs to pass a function to be applied to the facet strip text.
ggplot does have a nice ready function label_wrap_gen() that wrap the long labels.
By default ggpairs use the column names as labels, and those can't contain spaces. label_wrap_gen() need spaces to split the labels on multiple rows.
This is a solution:
library(ggplot2)
library(GGally)
df <- iris
colnames(df) <- make.names(c('Long colname',
'Quite long colname',
'Longer tha usual colname',
'I\'m not even sure this should be a colname',
'The ever longest colname that one should be allowed to use'))
ggpairs(df,
columnLabels = gsub('.', ' ', colnames(df), fixed = T),
labeller = label_wrap_gen(10))
I'm trying to label a plot in R with superscript. For example, I have a variable called label:
>label <- colnames(tmp.df);
>label
[1] "ColumnA" "Volume 100mm3", "ColumnC", etc.
I would like to have "3" in the "Volume 100mm3" as superscript in my plot label. I cannot use something like:
label <- c("ColumnA", expression(paste('Volume 100mm'^'3')), "ColumnC");
since the ordering of the column names in tmp.df may change from run to run. So how can I get around this problem?
You could find the one with the mm by
ind <- grep("mm",label)
splt <- strsplit(label[ind], "mm")[[1]]
and then inject the expression via
label[ind] <- parse(text=sprintf("paste('%smm'^'%s')",splt[1],splt[2]))
If there are multiple strings that indicate the need for expressions, then it should be straightforward to adapt this.
You can use bquote for this. The * connects "Volume 100" to "mm^3" without a space. If you want a space there, you can use ~ instead of *.
plot(1:10, main = bquote(.("Volume 100") * mm^3))
how about just using the Unicode character for cubed 'SUPERSCRIPT THREE' (U+00B3)? in R, this would be escaped as '100mm\u00B3', or if this is coming from a data file, just use unicode characters directly in the file.
I can't find a way how to write subscripts in the title or the subtitle in R.
How can I write v 1,2 with 1,2 as subscripts?
Thanks for your help!
expression is your friend:
plot(1,1, main=expression('title'^2)) #superscript
plot(1,1, main=expression('title'[2])) #subscript
If you are looking to have multiple subscripts in one text then use the star(*) to separate the sections:
plot(1:10, xlab=expression('hi'[5]*'there'[6]^8*'you'[2]))
See ?expression
plot(1:10,main=expression("This is a subscript "[2]))
A subscript and referring to a stored value...
a <- 10
plot(c(0,1), c(0,1), type = 'n', ann = FALSE, xaxt = 'n', yaxt = 'n')
text(0.2, 0.6, cex = 1.5, bquote(paste('S'['f']*' = ', .(a))))
Another example, expression works for negative superscripts without the need for quotes around the negative number:
title(xlab=expression("Nitrate Loading in kg ha"^-1*"yr"^-1))
and you only need the * to separate sections as mentioned above (when you write a superscript or subscript and need to add more text to the expression after).
As other users have pointed out, we use expression(). I'd like to answer the original question which involves a comma in the subscript:
How can I write v 1,2 with 1,2 as subscripts?
plot(1:10, 11:20 , main=expression(v["1,2"]))
Also, I'd like to add the reference for those looking to find the full expression syntax in R plotting: For more information see the ?plotmath help page. Running demo(plotmath) will showcase many expressions and relevant syntax.
Remember to use * to join different types of text within an expression.
Here is some of the sample output from demo(plotmath):