This question already has answers here:
Change colors in ggpairs now that params is deprecated
(2 answers)
Closed 1 year ago.
I'm using the ggpairs() function from the GGally package, but I'm having an issue dealing with overplotting.
Is there a good way to address the overplotting? I've tried setting the alpha value but I couldn't find out how to pass it to ggpairs(). I also looked into using geom_hex but again, I couldn't see how to use it with ggpairs().
Here's a simple example:
# Create example data
df <- data.frame(x1=rnorm(1e4),
x2=rnorm(1e4),
x3=runif(1e4))
# Pairs plot
GGally::ggpairs(df)
To reduce overplotting of the points you may modify the size aesthetic in point based layers displayed in the lower triangular of the plot matrix:
GGally::ggpairs(df, lower=list(continuous=GGally::wrap("points", size = .01)))
Related
This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 8 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example
This question already has answers here:
Heat map or density map in R
(3 answers)
Closed 7 years ago.
Say I have a simple matrix A generated by
A = matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
Now, my goal is to make a heatmap out of A, such that each number is replaced by a colour field. How to do that?
I tried
heatmap(A)
but it produced three coloured stripes instead of 9 distinct fields.
One of the possibilities that have not been described in the solutions referred to in the comments consists in using the pheatmap package:
library(pheatmap)
A <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
pheatmap(A, cluster_rows = FALSE, cluster_cols = FALSE)
which gives:
This question already has an answer here:
Needle plot in ggplot2 [duplicate]
(1 answer)
Closed 8 years ago.
I am trying to make a plot x,y stick plot (like the link in the bottom of the post). I am attempting to make a Mass spectrum plot (basically a stick plot), is there an easy way of doing this with ggplot2? I know how to make the plot with the plot() function in R, however, I would like to be able to make it with ggplot 2, as I need to do some additional modifications afterwards for which I require ggplot2.
So essentially I want to make a x,y stick plot. I am new to R, so bear with me.
Example of what I want
Try to upload some code and let us know where you are getting stuck. Also, please refer to the documentation or these examples.
That having been said, I believe this is close to what you want:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg)) +
geom_bar(binwidth = 1, color = 'white')
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 8 years ago.
I tried to finde a solution for my problem but I could not. Probably, my problem is easy for some of you. However, I need support. I would be greatful for any help.
I have made a ggplot for two factors: HGU_type and cycle_complexity to show their proportions:
I used:
g2<-ggplot(t,aes(x=HGU_type,fill = cycle_complexity))+ geom_bar(position="fill")
g2
The graph, I get looks as follow:
I want to have increasing order of the bars on the x-axis...first bar with "nod", second with "shake", third with "retr"...
I tried everything and cannot find the solution.
I would be grateful for a hint
As #Japp pointed out, it's always best to include a minimal reproducible example with your question. I created this data set
#sample data
set.seed(18)
t<-data.frame(
HGU_type=sample(c("jerk","nod","pro","retr","shake","tilt","turn"), 50, replace=T, prob=sample(7)),
cycle_complexity=sample(c("multiple", "single"), 50, replace=T)
)
And a plot like your original one is created by
ggplot(t,aes(x=HGU_type,fill = cycle_complexity))+ geom_bar(position="fill")
In order to change the order in which the bars are drawn, you need to change the levels of the factor used for the x-axis. The reorder() function makes it easy to reorder factors based on different properties. Here we will re-order based on the proportion of "multiple" in each group
t$HGU2<-reorder(t$HGU_type, t$cycle_complexity,FUN=function(x) mean(as.numeric(x)))
Then we can plot with
ggplot(t,aes(x=HGU2,fill = cycle_complexity))+ geom_bar(position="fill")
to get
This question already has answers here:
Colouring plot by factor in R
(6 answers)
Closed 9 years ago.
I am trying to make a scatter plot coloured by factor. I am using the following code:
data<-iris
plot(data$Sepal.Length, data$Sepal.Width, col=data$Species)
Is there anyway I can colour by the species factor but specify my own custom colours? Having a look around on Google it seems it is possible to do using ggplot2 but I have never used it and was hoping I could do this using the basic R functions.
Any help would be greatly appreciated!
You can manually set the R palette used by your plot call like so:
palette(c("blue","pink","green"))
Which you can reset like so:
palette("default")
Try it out, creating two plots, one with default colours, one with the new colours specified:
# default plotting
palette("default")
plot(iris$Sepal.Length, iris$Sepal.Width, col=iris$Species, pch=19)
# after specifying custom palette
palette(c("blue","pink","green"))
plot(iris$Sepal.Length, iris$Sepal.Width, col=iris$Species, pch=19)