I would like to reproduce the attached plot on the training iris dataset with Petal.Length (x) and Sepal.Length (y). I assume it is about stat_density_2d arguments, but I was not able to reach the exact same picture.
Any help is highly appreciated.
You need to map alpha to the calculated level within stat_density2d_filled, using alpha = after_stat(level) inside aes. Just tweak the breaks argument to define where you want the contour cutoffs to be. I also found that adjusting the h argument to set a slightly wider bandwidth than the default worked better than the default setting with this particular data set. Additionally, a call to scale_alpha_manual will give you fine control over the opacity of each band - here I set a sequence of six values between 0 and 1, since only the first 5 will be used, meaning that the most dense bands still won't be fully opaque and "overpower" the points.
library(ggplot2)
ggplot(iris, aes(Petal.Length, Sepal.Length, fill = Species)) +
stat_density2d_filled(aes(alpha = after_stat(level)),
h = c(1, 1), breaks = c(0, 0.03, 0.1, 0.25, 0.5, 5)) +
geom_point(shape = 21, size = 3) +
scale_alpha_manual(values = seq(0, 1, length = 6)) +
theme_minimal(base_size = 20) +
coord_equal(expand = FALSE) +
theme(legend.position = "none",
panel.border = element_rect(color = "gray80", fill = NA))
Related
assume the following MWE:
library(ggplot2)
library(ggthemes)
set.seed(100)
df <- data.frame(x = rnorm(50), y = rnorm(50))
ggplot(df, aes(x,y)) +
geom_point() +
theme_tufte() +
geom_rangeframe() +
scale_x_continuous(breaks = extended_range_breaks()(df$x),
labels = scales::number_format(accuracy = 0.1))+
scale_y_continuous(breaks = extended_range_breaks()(df$y),
labels = scales::number_format(accuracy = 0.1))
From my point of view the number of ticks on the y-axis is not enough, the space between 1.0 and 2.9 is too large and I would like to have another tick at 2.0. Anyone an idea how to do that when working with extended_range_breaks or do I have to switch to manually setting the ticks?
I tried scale_y_continuous(n.breaks = 7) and scale_y_continuous(breaks = extended_range_breaks(n = 7)(df$y) but both don't have an effect.
Not a perfect answer, but at least a work-around for now: change the weights applied to the four optimization components. The parameters are set to w = c(0.25, 0.2, 0.5, 0.05) for simplicity, coverage, density, and legibility. If we set coverage for example to 2 (for the y-axis), the graph changes to the following:
The actual desired goal to simly add (-2,2) was yet not possible with this tweak.
I am a newbie for stack Overflow and r language.
Here is my problem.
I now have a dataframe with one variable called Type and other 14 variables whose correlation matrix heatmap needed to be calculated.
origin dataset
I already have an overall format using ggplot2, and the theme is default theme_grey but fine for me to view. The code is :
m<- melt(get_lower_tri(round(cor(xrf[3:16], method = 'pearson', use = 'pairwise.complete.obs'), 2)),na.rm = TRUE)
ggplot(m, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = 'skyblue4',
high = 'coral2',
mid = 'white',
midpoint = 0,
limit = c(-1, 1),
space = "Lab",
name = 'Person\nCorrelation') +
theme_grey()+
coord_fixed() +
theme(axis.title = element_blank())
The result is fine and the background looks good to view.
But when I managed to generate a grouped correlation matrix heatmap, I found that no matter how hard I tried (using theme(panel.background = element_rect()) or theme(panel.background = element_blank())), the subplot backgrounds won’t change and remain this ugly grey which is even different from the overall one.
Here is my code:
Type = rep(c('(a)', '(b)', '(c)','(d)', '(e)', '(f)', '(g)', '(h)', '(i)', '(j)'), each = 14^2)
# Get lower triangle of the correlation matrix
get_lower_tri<-function(x){
x[upper.tri(x)] <- NA
return(x)
}
df2 <- do.call(rbind, lapply(split(xrf, xrf$Type),
function(x) melt(get_lower_tri(round(cor(x[3:16], method = 'pearson', use = 'pairwise.complete.obs'), 2)),na.rm = FALSE)))
my_cors <- cbind(Type,df2)
my_cors %>%
ggplot(aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = 'skyblue4',
high = 'coral2',
mid = 'white',
midpoint = 0,
limit = c(-1, 1),
space = "Lab",
name = 'Person\nCorrelation') +
theme_grey()+
coord_fixed() +
theme(axis.title = element_blank(),
panel.background = element_rect(fill = 'grey90',colour = NA))+
facet_wrap("Type",ncol = 5, nrow = 2)
Isn’t the facet subplot backgrounds the same as the overall one if using the same theme? And how can I change it?
Update:sorry! It’s my first time to raise a question and it’s not a good one!
xrf is my original dataset...But now I have figured out why thanks to Tjebo and those who comment my faulty questions.It’s very instructive to me!!
scale_fill_gredient2(...,na.value = 'transparent') will solve it.The default value of this parameter is "grey50" which I took as the background color.
I am truly sorry for asking such a silly question, and I really really appreciate you guys’s nice comment for a rookie! Thank you guys!
I'd like to plot some measures that have been standardized to z-scores. I want the size of the point in geom_point() to increase from 0 to 3, and also to increase from 0 to -3. I also want the colour to change from red, to blue. The trick is to get both to work together.
Here is an example that's as close as I can get to what I'd like, note that the size of the point increases from -2, whereas I want the size of the point to increase as the z_score moves away from zero.
library(tidyverse)
year <- rep(c(2015:2018), each = 3)
parameters <- rep(c("length", "weight", "condition"), 4)
z_score <- runif(12, min = -2, max = 2)
df <- tibble(year, parameters, z_score)
cols <- c("#d73027",
"darkgrey",
"#4575b4")
ggplot(df, aes(year, parameters, colour = z_score, size = z_score)) +
geom_point() +
scale_colour_gradientn(colours = cols) +
theme(legend.position="bottom") +
scale_size(range = c(1,15)) +
guides(color= guide_legend(), size=guide_legend())
bubble plot output
One trick I tried was to use the absolute value of z_score which scaled the points correctly but messed up the legend.
Here's what I'd like the legend and points size to be scaled to, though I'd like the colour to be a gradient as in my example. Any insight would be greatly appreciated!
Link to plot legend
You were very close. In order to adjust the size of the points in the legend, use the override.aes option in the guides function.
library(ggplot2)
year <- rep(c(2015:2018), each = 3)
parameters <- rep(c("length", "weight", "condition"), 4)
z_score <- runif(12, min = -2, max = 2)
df <- tibble(year, parameters, z_score)
cols <- c("#d73027", "darkgrey", "#4575b4")
ggplot(df, aes(year, parameters, colour = z_score)) +
geom_point( size=abs(5*df$z_score)) + # times 5 to increase size
scale_colour_gradientn(colours = cols) +
theme(legend.position="bottom") +
scale_size(range = c(1,15)) +
guides(color=guide_legend(override.aes = list(size = c( 5, 1, 5))) )
In order to suppress the legend being print for the size attribute, I moved it outside the aes, field. This works for this example, one will have to adjust the size=c(...) to match the number of division in the legend.
This should answer your question and get you most of the way there on answering your question.
I would like to draw a chart with ggplot for a couple of model accuracies. The detail of the plotted result doesn't matter, however, I've a problem to fill the geom_point objects.
A sample file can be found here: https://ufile.io/z1z4c
My code is:
library(ggplot2)
library(ggthemes)
Palette <- c('#A81D35', '#085575', '#1DA837')
results <- read.csv('test.csv', colClasses=c('factor', 'factor', 'factor', 'numeric'))
results$dates <- factor(results$dates, levels = c('01', '15', '27'))
results$pocd <- factor(results$pocd, levels = c('without POCD', 'with POCD', 'null accuracy'))
results$model <- factor(results$model, levels = c('SVM', 'DT', 'RF', 'Ada', 'NN'))
ggplot(data = results, group = pocd) +
geom_point(aes(x = dates, y = acc,
shape = pocd,
color = pocd,
fill = pocd,
size = pocd)) +
scale_shape_manual(values = c(0, 1, 3)) +
scale_color_manual(values = c(Palette[1], Palette[2], Palette[3])) +
scale_fill_manual(values = c(Palette[1], Palette[2], Palette[3])) +
scale_size_manual(values = c(2, 2, 1)) +
facet_grid(. ~ model) +
xlab('Date of knowledge') +
ylab('Accuracy') +
theme(legend.position = 'right',
legend.title = element_blank(),
axis.line = element_line(color = '#DDDDDD'))
As a result I get unfilled circles and squares. How can I fix it, so that the squares and circles are filled with the specfic color?
Additional question: I would like to add a geom_line to the graph, connecting the three points in each group. However, I fail to adjust linetype and width. It always take the values of scale_*_manual, which is very adverse especially in the case of size.
Thanks for helping!
You need to change the shapes specified, like so:
scale_shape_manual(values = c(21,22,23)) +
For your additional question, that should be solved if you set aes(size=) in the first part of your code (under ggplot(data=...) and then manually specify size=1 under geom_line as +geom_line(size=1....`
I produce these two graphs using p-values obtained from a pairwise.wilcox.test and the following script. My problem is that I want to have the same colour for the different breaks in both graphs for comparison purposes. I’m aware that the problem here is that in the first graph (Sum of ROH) I don’t have any value in the break (0.001,0.05]. However I want to force the graph to add this break in the legend and to have the same colour as the second graph (Mean ROH Size)
test.result$value<-cut(test.result$value, breaks=c(-Inf,0.001,0.05,1),right=T)
windows()
ggplot(data = test.result, aes(X1, X2, fill = value))+
ggtitle("Sum of ROH")+
xlab("")+
ylab("")+
geom_tile(aes(fill=test.result$value),color="white")+
scale_fill_brewer(palette="Blues", direction = -1,name="p-Val")+
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
Set the levels of your factors to be the same. You can add levels that don't exist.
For example
levels(test.result$value) = c("(-Inf,0.001]", "(0.001,0.05]", "(0.05,1]")
Then add drop = FALSE into your fill scale to keep the value in the legend.
scale_fill_brewer(palette = "Blues", direction = -1, drop = FALSE, name = "p-Val")