How should I change the size of symbols in legends? I checked the document of theme
but found no answer.
Here is an example:
library(ggplot2);library(grid) set.seed(1000) x <- 1:6 mu <- sin(x) observed <- mu + rnorm(length(x), 0, 0.5*sd(mu)) data <- data.frame( t=rep(x, 2), value=c(mu, observed) - min(mu, observed) + 0.5, class = rep(c("mu", "observed"), each=length(x))) mu <- data$value[1:length(x)] observed <- data$value[1:length(x) + length(x)] mu.min <- mu - 3 * 0.5 * sd(mu) mu.max <- mu + 3 * 0.5 * sd(mu) g <- ggplot(data=data) g <- g + geom_point(aes(x=value, y=t, shape=class, size=24)) + scale_size(guide="none") g <- g + scale_shape(name="", labels=expression(paste(S[u](t), ", the observation at time ", t), paste(mu[u](t), ", the mean of ", tilde(S)[u](t), " "))) stat_function.color <- gray(0.5) g <- g + geom_segment(aes(y=1:6, yend=1:6, x=mu.min, xend=mu.max, linetype="2", alpha = 1), color=stat_function.color) + scale_alpha(guide="none") + scale_linetype(name= "", labels=expression(paste("probability density function (pdf) of ", tilde(S)[u], " at time ", t))) for(i in 1:length(x)) { g <- g + stat_function(fun=function(x, i) { ifelse( x <= mu.max[i] & x >= mu.min[i], dnorm(x, mu[i], sd(mu)) + i, NA) }, color=stat_function.color, args=list(i=i)) } background.color <- gray(0.75) g <- g + theme( axis.text=element_blank(), title=element_text(size=rel(1.5)), legend.text=element_text(size=rel(1.5)), legend.position="top", legend.direction="vertical", # legend.key.size = unit(2, "cm"), panel.background=element_rect(fill=background.color), panel.grid.major=element_line(color=background.color), panel.grid.minor=element_line(color=background.color) ) + coord_flip() plot(g)
You can use function guides() and then with argument override. aes= set line size= (width) to some large value. To remove the grey area around the legend keys set fill=NA for legend. key= inside theme() .
To change the Size of Legend, we have to add guides() and guide_legend() functions to the geom_point() function. Inside guides() function, we take parameter color, which calls guide_legend() guide function as value. Inside guide_legend() function, we take an argument called override.
You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))
You should use:
theme(legend.key.size = unit(3,"line"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With