guide_legend and ggplot2, format nrow - r

I am trying to format an over-long legend on a ggplot so that there is a maximum no. of rows. I've read all the documentation that I could find, especially this: http://docs.ggplot2.org/0.9.3.1/guide_legend.html but for some reason, the legend will not format.
I've given a reproducible sample below using the quakes dataset, and converted the column stations to character so that they plot individually (otherwise, they seem to plot as groups).
plotquakes <- function(magreq) {
library(ggplot2)
magdata <- subset(quakes, mag > magreq)
magdata$stations <- as.character(magdata$stations)
g <- ggplot(magdata, aes (x = lat, y = long))
g + geom_point(aes(alpha = stations), fill = "black", pch=21, size = 6) +
labs(x = "Latitude", y = "Longitude") +
geom_vline(xintercept = 0, col = "red") +
geom_hline(yintercept = 0, col = "red") +
guides(col = guide_legend(nrow = 16))
}
plotquakes(5)
And what I get is this:
whereas I would like to have a maximum of 16 data fields per column in the legend.

You are changing the wrong guide.
plotquakes <- function(magreq) {
library(ggplot2)
magdata <- subset(quakes, mag > magreq)
magdata$stations <- as.character(magdata$stations)
g <- ggplot(magdata, aes (x = lat, y = long))
g + geom_point(aes(alpha = stations), fill = "black", pch=21, size = 6) +
labs(x = "Latitude", y = "Longitude") +
geom_vline(xintercept = 0, col = "red") +
geom_hline(yintercept = 0, col = "red") +
guides(alpha = guide_legend(nrow = 16)) #note it's alpha not col
}
plotquakes(5)

Related

Customize the position of `geom_rug`

Below is a working example
library(ggplot2)
set.seed(926)
df <- data.frame(expression = rnorm(900),
time = c(rnorm(300), rnorm(300, 1, 2), rnorm(300, 2,0.5)),
membership = factor(rep(1:3, each = 300)))
ggplot(df, aes(x = time, y = expression, fill = membership)) +
geom_point(shape=21, size = 3) +
geom_rug(data = subset(df, membership ==3), sides = "b", color = "green", length = unit(1.5, "cm")) +
geom_rug(data = subset(df, membership ==2), sides = "b", color = "blue", length = unit(1, "cm")) +
geom_rug(data = subset(df, membership ==1), sides = "b", color = "red") +
scale_y_continuous(expand = c(0.3, 0))
My hope is something like
.
Note that I know the options of outside = TRUE, side = "tb" out there. But placing all rug plots at the bottom is what I really hope for.
geom_rug is designed to be drawn at the margins of a plot. It's probably best to use geom_point with a custom symbol in this case:
ggplot(df, aes(x = time, y = expression, fill = membership)) +
geom_point(shape=21, size = 3) +
geom_point(aes(y = -as.numeric(membership) - 2.5, color = membership),
shape = "|", size = 8) +
geom_hline(yintercept = -3) +
theme_classic(base_size = 20) +
scale_y_continuous(breaks = c(-2, 0, 2))
I don't think the position of geom_rug() can be easily customised. I'd recommend to use geom_segment() instead to draw the rugs like you'd want them.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2
set.seed(926)
df <- data.frame(expression = rnorm(900),
time = c(rnorm(300), rnorm(300, 1, 2), rnorm(300, 2,0.5)),
membership = factor(rep(1:3, each = 300)))
# Helper variables
limits <- range(df$expression)
step <- diff(limits) * 0.1
size <- 0.45 * step
ggplot(df, aes(x = time, y = expression, fill = membership)) +
geom_point(shape=21, size = 3) +
geom_segment(
aes(
colour = membership,
xend = time,
y = limits[1] - as.numeric(membership) * step + size,
yend = limits[1] - as.numeric(membership) * step - size
)
)
Created on 2022-12-12 by the reprex package (v2.0.1)

ggplot trying to make a Cleveland plot but I cannot get a legend

library(ggplot2)
library(ggthemes)
data <- read.csv('/Users/zbhay/Documents/r-data.csv', header = 1)
zb <- ggplot(data) +
geom_segment( aes(x=x, xend=x, y=value1, yend=value2), color="black")+
geom_point( aes(x=x, y=value1), color=rgb(0.2,0.7,0.1,1), size=4 )+
geom_point( aes(x=x, y=value2), color=rgb(0.7,0.2,0.1,1), size=4 )+
coord_flip() +
theme_solarized() +
scale_y_continuous(breaks = seq(0, 10000, by = 500)
)
zb + labs(title = "Title",
subtitle = "subtitle") +
xlab("Business Functions") +
ylab("# of hours")
legend("left", c("Starting", "Ending"),
box.col = "darkgreen"
)
Hello, here is the code. The CSV file is structured as follows; column A = names, column b = starting number, column c = final number.
I am trying to set up a legend that calls out the final number vs starting number. I have tried and tried but cannot seem to be able to crack it. If anyone knows a fix, I would appreciate it if you could let me know.
As a general rule when using ggplot2 you have to map on aesthetics if you want to get a legend, i.e. instead of setting the colors for your points as arguments map a value on the color aes, e.g. in my code below I map the constant value or category start on the color aes inside aes() for the first geom_point. Afterwards you could use scale_color_manual to assign your desired colors and labels to these "categories" or "values". Finally, the color of the legend box could be set via the theme option legend.background. However, the legend keys themselves have a background color too, which I set to NA via legend.key.
Using some fake random example data:
library(ggplot2)
library(ggthemes)
set.seed(123)
data <- data.frame(x = letters[1:5], value1 = runif(5, 0, 10000), value2 = runif(5, 0, 10000))
ggplot(data) +
geom_segment(aes(x = x, xend = x, y = value1, yend = value2), color = "black") +
geom_point(aes(x = x, y = value1, color = "start"), size = 4) +
geom_point(aes(x = x, y = value2, color = "end"), size = 4) +
coord_flip() +
theme_solarized() +
scale_y_continuous(breaks = seq(0, 10000, by = 500)) +
scale_color_manual(values = c(start = rgb(0.2, 0.7, 0.1, 1), end = rgb(0.7, 0.2, 0.1, 1)), labels = c(start = "Starting", end = "Ending")) +
labs(title = "Title", subtitle = "subtitle", x = "Business Functions", y = "# of hours", color = NULL) +
theme(
legend.key = element_rect(fill = NA),
legend.background = element_rect(fill = "darkgreen")
)

Legend does not fit well ggplot density

I am doing a plot of densities, I want to add a legend but is overlapped with the symbol. The code is hereunder:
dfGamma = data.frame(a = rgamma(100,shape = 7.1,rate= 0.0055),
b = rgamma(100, shape = 10,rate= 0.0055),
c = rgamma(100, shape = 7.1,rate= 0.0055))
dfGamma = stack(dfGamma)
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
ggtitle("Gamma distribution")+theme(legend.position="right")+
scale_color_manual(labels = c(expression(paste(alpha,"=7.1 ",beta,"=0.0055")),
expression(paste(alpha,"= 10 ",beta,"=0.0055")),
expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
values = c('red', 'blue',"green"))
p
the plot is:
The guides option, guide_legend is what you need. You can read more about it in the ggplot reference. Does this help?
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
ggtitle("Gamma distribution")+
theme(legend.position="right") +
scale_color_manual(labels = c(expression(paste(alpha, "=7.1 ", beta, "=0.0055")),
expression(paste(alpha,"= 10 ",beta,"=0.0055")),
expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
values = c('red', 'blue',"green")) +
guides(colour = guide_legend(label.position = "bottom"))
p

How to merge legends for color and shape when geom_hline has a separate (additional) entry in the color legend?

I have the following code, which produces the following plot:
cols <- brewer.pal(n = 3, name = 'Dark2')
p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() +
geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) +
geom_line(position=pd) +
geom_point(aes(shape=Approach, colour = Approach), size = 4) +
geom_hline(aes(yintercept = cp.best$slope, colour = "C2P"), show_guide = FALSE) +
scale_color_manual(name="Approach", breaks=c("C2P", "P2P", "CP2P"), values = cols[c(1,3,2)]) +
scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal',
legend.position = 'top',
plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"),
text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))
p4 <- p4 + guides(colour=guide_legend(override.aes=list(shape=c(NA,17,16))))
p4
When I try show_guide = FALSE in geom_point, the shape of the point in the upper legend are all set to default solid circles.
How can I make the lower legend to disappear, without affecting the upper legend?
This is a solution, complete with reproducible data:
library("ggplot2")
library("grid")
library("RColorBrewer")
cp2p <- data.frame(xval = 10 * 2:15, yval = cumsum(c(0.55, rnorm(13, 0.01, 0.005))), Approach = "CP2P", stringsAsFactors = FALSE)
p2p <- data.frame(xval = 10 * 1:15, yval = cumsum(c(0.7, rnorm(14, 0.01, 0.005))), Approach = "P2P", stringsAsFactors = FALSE)
pd <- position_dodge(0.1)
cp.best <- list(slope = 0.65)
all.m <- rbind(p2p, cp2p)
all.m$Approach <- factor(all.m$Approach, levels = c("C2P", "P2P", "CP2P"))
all.m$se <- rnorm(29, 0.1, 0.02)
all.m[nrow(all.m) + 1, ] <- all.m[nrow(all.m) + 1, ] # Creates a new row filled with NAs
all.m$Approach[nrow(all.m)] <- "C2P"
cols <- brewer.pal(n = 3, name = 'Dark2')
p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() +
geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) +
geom_line(position=pd) +
geom_point(aes(shape=Approach, colour = Approach), size = 4, na.rm = TRUE) +
geom_hline(aes(yintercept = cp.best$slope, colour = "C2P")) +
scale_color_manual(values = c(C2P = cols[1], P2P = cols[2], CP2P = cols[3])) +
scale_shape_manual(values = c(C2P = NA, P2P = 16, CP2P = 17)) +
scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal',
legend.position = 'top',
plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"),
text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))
p4
The trick is to make sure that all of the desired levels of all.m$Approach appear in all.m, even if one of them gets dropped out of the graph. The warning about the omitted point is suppressed by the na.rm = TRUE argument to geom_point.
Short answer:
Just add a dummy geom_point layer (transparent points) where shape is mapped to the same level as in geom_hline.
geom_point(aes(shape = "int"), alpha = 0)
Longer answer:
Whenever possible, ggplot merges / combines legends of different aesthetics. For example, if colour and shape is mapped to the same variable, then the two legends are combined into one.
I illustrate this using simple data set with 'x', 'y' and a grouping variable 'grp' with two levels:
df <- data.frame(x = rep(1:2, 2), y = 1:4, grp = rep(c("a", "b"), each = 2))
First we map both color and shape to 'grp'
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4)
Fine, the legends for the aesthetics, color and shape, are merged into one.
Then we add a geom_hline. We want it to have a separate color from the geom_lines and to appear in the legend. Thus, we map color to a variable, i.e. put color inside aes of geom_hline. In this case we do not map the color to a variable in the data set, but to a constant. We may give the constant a desired name, so we don't need to rename the legend entries afterwards.
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4) +
geom_hline(aes(yintercept = 2.5, color = "int"))
Now two legends appears, one for the color aesthetics of geom_line and geom_hline, and one for the shape of the geom_points. The reason for this is that the "variable" which color is mapped to now contains three levels: the two levels of 'grp' in the original data, plus the level 'int' which was introduced in the geom_hline aes. Thus, the levels in the color scale differs from those in the shape scale, and by default ggplot can't merge the two scales into one legend.
How to combine the two legends?
One possibility is to introduce the same, additional level for shape as for color by using a dummy geom_point layer with transparent points (alpha = 0) so that the two aesthetics contains the same levels:
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4) +
geom_hline(aes(yintercept = 2.5, color = "int")) +
geom_point(aes(shape = "int"), alpha = 0) # <~~~~ a blank geom_point
Another possibility is to convert the original grouping variable to a factor, and add the "geom_hline level" to the original levels. Then use drop = FALSE in scale_shape_discrete to include "unused factor levels from the scale":
datadf$grp <- factor(df$grp, levels = c(unique(df$grp), "int"))
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4) +
geom_hline(aes(yintercept = 2.5, color = "int")) +
scale_shape_discrete(drop = FALSE)
Then, as you already know, you may use the guides function to "override" the shape aesthetics in the legend, and remove the shape from the geom_hline entry by setting it to NA:
guides(colour = guide_legend(override.aes = list(shape = c(16, 17, NA))))

How do I remove the _printed_ output warnings using ggplot2 with knitr

GGplot2 prints out a warning when using scale_colour_gradient twice in a plot, which I cannot suppress in knitr. Here is a screenshot of my browser after knitting an RHTML file:
I need to colour gradients, one for the line (cheap / dear of yield curve) and one for the instruments which is similar but subtly different (coloured dots).
Here is my ggplot code:
ggp <- ggplot(polys, aes(x = xvals, y = yvals)) +
#geom_polygon(aes(fill = - value, group = id, alpha = value)) + # lovely blue
geom_polygon(aes(fill = value, group = id, alpha = value)) + # lovely shiny light blue middle draw me in
scale_x_log10(breaks = xaxtickpos, minor_breaks = NULL) +
theme(legend.position = "none", panel.background = element_rect(fill = "grey85", colour = NA)) +
xlab("maturity") + ylab("bps")
ggp <- ggp + geom_line(data = quanmelt[quanmelt[, "percentile"] %in% outerthresh, ],
aes(x = mat, y = value, group = percentile), colour = "white", size = 2)
ggp <- ggp + geom_line(data = quanmelt[quanmelt[, "percentile"] %in% innerthresh, ],
aes(x = mat, y = value, group = percentile), colour = "white", size = 1,
linetype = "dotted")
#add last few days line/today (this doesn't work very well hence commented out)
todayback <- todayline[todayline$daysback == 2, ] # get this historic lines
ggp <- ggp + geom_smooth(data = todayback, aes(x = mat, y = value, group = daysback),
colour = "darkred", linetype = "dashed",
se = FALSE, size = 1, method = "loess", span = (ifelse(smooth, 0.3, 0.1)))
#add boxplot
ggp <- ggp + geom_boxplot(data = meltcdlong, aes(x = mat, y = value, group = bond), outlier.size = NA,
colour = "grey30", alpha = 0.5, size = 0.2, width = 0.025)
# add the latest point
ggp <- ggp + geom_point(data = latestcdpoint, aes(x = mat, y = value, group = bond))
# now do labels (twice - one for above, one for below)
ggp <- ggp + geom_text(data = latestcdpoint[latestcdpoint$adjustvertvec == 1, ], aes(x = mat, y = labelposies, label = label),
angle = 90, colour = "grey20", size = 3, hjust = 0, alpha = 0.5)
ggp <- ggp + geom_text(data = latestcdpoint[latestcdpoint$adjustvertvec == 0, ], aes(x = mat, y = labelposies, label = label),
angle = 90, colour = "grey20", size = 3, hjust = 1, alpha = 0.5)
#now print a nice z-score graded colour line for the curve
todaytoday <- todayline[todayline$daysback == 0, ]
minz <- min(rescale(todaytoday[, "zscore"])) # for scaling of z-score line gradient colours
maxz <- max(rescale(todaytoday[, "zscore"]))
bpspline <- smooth.spline(todaytoday$mat, todaytoday$value, spar = 0.4) # Smooth out the curve with lots of points
zscorespline <- smooth.spline(todaytoday$mat, todaytoday$zscore) # and smooth out the zscores too
xplot <- seq(2, maxmat, by = 0.1)
todayplotter <- data.frame(mat = xplot, value = predict(bpspline, xplot)$y,
zscore = rescale(c(-5, 5, predict(zscorespline, xplot)$y))[-1:-2]) # build the plotter
ggp <- ggp + geom_path(data = todayplotter, aes(x = mat, y = value, colour = zscore), size = 2, linejoin = "bevel") +
scale_colour_gradientn(colours = gradientcolours, values = gradientscale, limits = c(minz, maxz))
#and the title
ggp <- ggp + ggtitle(cCode)
# now the test chart
mm <<- meltcdrecent[meltcdrecent$daysback == 0, ]
ggp <- ggp + geom_point(data = mm, aes(x = mat, y = value, colour = rescale(c(-5, 5, zscore))[-1:-2]), size = 6) +
scale_colour_gradientn(colours = gradientcolours, values = gradientscale, limits = c(0, 1))
ggp <- ggp + geom_point(data = mm, aes(x = mat, y = value), colour = "black", size = 4.5)
ggp <- ggp + geom_text(data = mm, aes(x = mat, y = value), label = round(mm$zscore, 1), colour = "white", size = 2, alpha = 0.7)
It's quite complex, but you can see I have two scale_colour_gradient(s).
Here is my knitr code:
<!--begin.rcode changer, echo=FALSE, fig.height=4.5, fig.width=8
for(x in ac) {
g <- ggCD(x, plotit = FALSE)
suppressWarnings(plot(g$cdChart))
}
end.rcode-->
I would like either to get rid of these warnings (they're not actual real warnings, so suppressWarnings doesn't work), or else, use scale_colour_gradient in a way which does not produce this text in the first place.
Change
<!--begin.rcode changer, echo=FALSE, fig.height=4.5, fig.width=8
into
<!--begin.rcode changer, echo=FALSE, fig.height=4.5, fig.width=8, message=FALSE

Resources