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))
Related
I'm trying to achieve two things in a ggplot label, being to paste an assigned character variable as well as some text with superscript into the same label. However, I've only been able to do either of those two things but I can't seem to do both at the same time.
name = "Mary"
ggplot() + ylab(expression(paste(name~"BMI"~kg/mm^2))) #this can only do the superscript but not get the name
ggplot() + ylab(paste(name,expression("BMI"~kg/mm^2))) #this can get the name but not the superscript
Any help is appreciated, thanks!
Use bquote
ggplot() + ylab(bquote(.(name)~BMI~kg/mm^2))
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 have data.frame list where its column named with rather complicated string, and that need to be plotted by using facet_wrap. I intend to manipulate the this complicated string and plot in facet_grid. I got some idea to split the string by using \n, but in resulted plot, my desired label still is not fully displayed. Can anyone give me some idea how to plot the data.frame if label the column named with long character? How can I achieve my desired plot ?
reproducible data.frame :
myList <- list(
wmTncofSydhTfbsK90cmxcgMnP30SxEAlnTest1.saved=
data.frame(GRP=c("up","up","down"), Name=rep("wmTncofSydhTfbsK90cmxcgMnP30SxEAlnTest1", 3),
v1=c(2,7,9)),
wmTncofSydhTfbsK90cmxcgMnP30SxEAlnTest2.saved=
data.frame(GRP=c("up","down","up","down"), Name=rep("wmTncofSydhTfbsK90cmxcgMnP30SxEAlnTest2", 4),
v1=c(13,8,11,3))
)
This is my imaginative desired plot :
How can I get this desired plot ? How can I plot the data.frame if it named with unexpected long character ? Thanks in advance
Edit :
threshold for getting up, downgroup, we can set threshold threshold >9for up; otherwise down. sorry for this missing point.
but how can I continue this code where I am trying to manipulate that long string column in data ? Can anyone point me how to proceed my approach and produce my desired plot ?
Here is some sample code to correct the problem by splitting the column A into lines of at most 26 characters:
library(stringr)
library(ggplot2)
df <- data.frame(A = c(paste(rep(LETTERS, 3), collapse = ""), paste(rep(letters, 3), collapse = "")), B = 3)
ggplot(df, aes(x = B)) + facet_wrap(~A) + geom_bar()
while(any(str_detect(df$A, "(?<=(^|\\n))[\\n]{26}(?=[^\\n])"))) {
df %<>% mutate(A = str_replace(A, "(?<=(^|\\n))[\\n]{26}(?=[^\\n])", "\\0\n"))
}
ggplot(df, aes(x = B)) + facet_wrap(~A) + geom_bar()
The core is repeatedly replacing the regex pattern (?<=(^|\n))[^\n]{26}(?=[^\\n]) wich matches a 26-character (chose at your own discretion) length substring wich is not immediately succeeded by a newline and is preceeded by the beginning of the string or by a newline.
I suggest stepping through the while loop to understand how the regex works.
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)
In ggplot2, how do I refer to a variable name with spaces?
Why do qplot() and ggplot() break when used on variable names with quotes?
For example, this works:
qplot(x,y,data=a)
But this does not:
qplot("x","y",data=a)
I ask because I often have data matrices with spaces in the name. Eg, "State Income". ggplot2 needs data frames; ok, I can convert. So I'd want to try something like:
qplot("State Income","State Ideology",data=as.data.frame(a.matrix))
That fails.
Whereas in base R graphics, I'd do:
plot(a.matrix[,"State Income"],a.matrix[,"State Ideology"])
Which would work.
Any ideas?
Answer: because 'x' and 'y' are considered a length-one character vector, not a variable name. Here you discover why it is not smart to use variable names with spaces in R. Or any other programming language for that matter.
To refer to variable names with spaces, you can use either hadleys solution
a.matrix <- matrix(rep(1:10,3),ncol=3)
colnames(a.matrix) <- c("a name","another name","a third name")
qplot(`a name`, `another name`,data=as.data.frame(a.matrix)) # backticks!
or the more formal
qplot(get('a name'), get('another name'),data=as.data.frame(a.matrix))
The latter can be used in constructs where you pass the name of a variable as a string in eg a loop construct :
for (i in c("another name","a third name")){
print(qplot(get(i),get("a name"),
data=as.data.frame(a.matrix),xlab=i,ylab="a name"))
Sys.sleep(5)
}
Still, the best solution is not to use variable names with spaces.
Using get is not more "formal", actually I would argue the opposite. As the R help says (help("`")), you can almost always use a variable name that contains spaces, provided it's quoted. (Normally, with a backtick, as already suggested.)
Something similar was asked on ggplot2 mailing list and Mehmet Gültaş linked to this post. Another way of using strings to construct your ggplot call is through the aes_strings function. Note that you still have to put backticks inside the quotes for the thing to work for variables with spaces.
library(ggplot2)
names(mtcars)[1] <- "em pi dzi"
ggplot(mtcars, aes_string(x = "cyl", y = "`em pi dzi`")) +
theme_bw() +
geom_jitter()