I am ploting a multiple histogram for 5 variables, and I have the same title repeated along all of them. I didn´t find a way to personalize it for each histogram. Is there a way to do it? I am using the psych R package.
Maybe another package to recommend?
The code line:
multi.hist(AutosCompleteNorm[,11:15],main="bah")
And "bah" is repeated 5 times. I tried c("a","b",..."e") as an argument but it didn´t work.
Package documentation:
https://www.rdocumentation.org/packages/psych/versions/1.8.3.3/topics/multi.hist
Thanks!
The current psych package doesn't seem to support this, which is strange because it would have been a natural feature for such a plot.
For customizability, I recommend you to take a look at ggplot and then layout in the format you wish using gridExtra.
Here's the code to create the few histogram in ggplot:
library(ggplot2)
p1 <- ggplot(vids, aes(x=log(likes)))+geom_histogram()+labs(title="title1")
p2 <- ggplot(vids, aes(x=log(dislikes)))+geom_histogram()+labs(title="title2")
p3 <- ggplot(vids,
aes(x=log(comment_count)))+geom_histogram()+labs(title="title3")
And then laying them up in a 2 row layout (nrow=2):
library(gridExtra)
grid.arrange(p1, p2, p3, nrow = 2)
Changing the layout to nrow=1:
Following these comments, I just updated psych so that multi.hist is more useful.
You can now specify the margins for the plots, and it will, by default label each plot with the variable name.
I have not pushed the development version of psych (1.8.9) to CRAN yet, but it is available on my repository at
install.packages("psych",repos="https://personality-project.org/r",type="source")
Related
How can I obtain something similar to a mosaic plot but representing just the information from a frequency table for a single variable?
mosaicplot(table(my_var)) works fine, but only shows vertical bars.
Is it possible to obtain a mosaic plot like a puzzle of different tiles instead of just vertical bars? Something similar to this image:
I had the same question, today. I found out that the graph is called treemap and at least two libraries support creating it: treemap and plotly.
As #Man.A noted, you can create a treemap with the treemap R package. A simple example:
# library
library(treemap)
#> Warning: package 'treemap' was built under R version 4.1.3
# Create data
group <- c("group-1","group-2","group-3")
value <- c(13,5,22)
data <- data.frame(group,value)
# treemap
treemap(data,
index="group",
vSize="value",
type="index"
)
I am trying to plot a table with the values from a dataframe. I am not sure of what approach to take using ggplot2.
I am also trying one approach using gridextra::grid.table() and tableGrob() functions and both are working similarly.
For example: I plot iris as a table using following code
library(gridExtra)
library(grid)
d <- head(iris, 3)
g <- tableGrob(d)
grid.draw(g)
When I plot this data using grid.draw, why I am getting row numbers 1 to 3 along with the table and how can I work on removing these numbers and change font type and background colors. Please guide!
Also guide me to the approach using ggplot2
Your help will be appreciated!
I am looking for a solution on how to raise numbers in the labels of my vcd mosaic plot.
edit:
as an example i randomly selected the Sex label in the Titanic dataset:
vnames <- list(set_varnames=c(Sex="Sex=10^X"))
mosaic(Titanic, labeling_args=vnames)
They always will be displayed as 10^2 and not as 10².
For example working with expression(10^{2}) or
xlab(bquote('Zoospores ('*10^x*') per plastic box'))
in the normal R plots or ggplot2 does not work for the set_varnames= call in labeling_args= in the mosaic()-command of the vcd package.
I could not find an answer to my specific problem in the vcd mosaic plot, only answers regarding labeling in ggplot2 and normal plots..
looking forward to read from you guys !
With the hints of two dedicated members of the statistician community - #Achim Zeileis and David Meyer, i was able to find a rather simple solution.
To stick with the example:
vnames <- list(set_varnames=c(Sex=""))
mosaic(Titanic, labeling_args=vnames)
grid.text(bquote('Sex ('*10^X*') example'), y=0.9, x=0.46,gp=gpar(fontsize=21))
grid.text() did the job. Since you basically add the label afterwards you have to play a little with x and y to get it in place.
all the best,
Alexander
I've been trying to edit some plots created by ggplot2 using the functions provided by the packages grid and gridExtra. I realize that ggplot2 alone can make some really nice multifaceted plots. However, in some instances I like to create individual plots and then combine then together later on. While trying to do just that, I came across some unexpected behavior using cbind() with grid.draw() or grid.arrange() when using a ggplot2 graph that had been edited. Below is the code for an MWE:
#Load libraries
library(ggplot2); library(gridExtra)
#Load data
data(mtcars)
#Ggplot
p = qplot(wt,mpg,data=mtcars,color=cyl)
grob = ggplotGrob(p)
#Bold xlabel
grobEdited = editGrob(grid.force(grob),gPath("xlab","GRID.text"),grep=TRUE,gp=gpar(fontface="bold"))
#Visualize
grid.newpage()
grid.draw(grobEdited)
It worked as expected. Now to illustrate the issue, lets cbind() two of the same edited ggplot2 graphs:
#Cbind example with edited graphs
grid.newpage()
grid.draw(cbind(grobEdited,grobEdited))
It didn't work as expected! Now test cbind() on the unedited graphs:
#Cbind example with grob
grid.newpage()
grid.draw(cbind(grob,grob))
Works as expected. I'm new to gridded figures, so is there something I'm doing wrong?
I'm posting an answer following the comment from #user20650. The easiest workaround is to cbind() the ggplot2 graphs before editing them using the editing functions provided by grid or gridExtra:
#Edit after cbind()
grobEdited = editGrob(grid.force(cbind(grob,grob)),gPath("xlab","GRID.text"),global=TRUE,grep=TRUE,gp=gpar(fontface="bold"))
#Visualize
grid.newpage()
grid.draw(grobEdited)
Example:
library(xts)
data(sample_matrix)
matrix_xts <- as.xts(sample_matrix, dateFormat='Date')
matrix_xts[,1] = matrix_xts[,1] * 100
plot(matrix_xts)
If i plot this it will be hard to visualize the data. Is it possible somehow to have the first column be plotted on the secondary y -axis in xts.plot?
To my knowledge this isn't supported with xts plotting. (Also some people do not favour plotting with 2 different axes on a single plot for a variety of reasons such as it could be seen as misleading.)
Here is one work around. Plot the columns with different scales on different plots:
# Get latest version of xts for nice new plot tools (>= 0.10 not yet on CRAN)
library(devtools)
install_github("joshuaulrich/xts")
plot(matrix_xts[, 2:4])
lines(matrix_xts[,1], on = NA)
?plot.xts for xts version >=0.10 has some helpful examples for options related to the plot like colours, etc.