my original code
my_graph10 <- ggplot(Adata, aes(x = SVL, y = Fi)) + geom_point(aes(color = Morph)) + labs(x = "SVL (mm)", y = "Front Inner Limb (mm)") + geom_smooth(method=lm,se=FALSE,aes(color = Morph,linetype = Morph)) + scale_color_manual(values = c("orange", "steelblue")) results in this
Legend NA
after reading online, many said to use na.translate = F ; therefore I added this to the code
my_graph11 <- ggplot(Adata, aes(x = SVL, y = Fo)) + geom_point(aes(color = Morph)) + labs(x = "SVL (mm)", y = "Front Outer Limb (mm)") + geom_smooth(method=lm,se=FALSE,aes(color = Morph,linetype = Morph)) + scale_color_manual(**na.translate = F**, values = c("orange", "steelblue") and I am left with this
Two Legends
However, when I do so, it removes the NA from the original legend, but adds a new legend for linetype, under which NA is still listed. I attempted to do the same code for linetype but receive this error message "Error: Insufficient values in manual scale. 2 needed but only 0 provided"
You can use remove_missing
# Let's create some sample data first
library(ggplot2)
set.seed(2020)
x <- seq(0, 1, length.out = 100)
df <- data.frame(
x = x,
y = rnorm(length(x)),
Morph = sample(c("S", "U", NA), length(x), replace = TRUE))
# Use `remove_missing` with `na.rm = TRUE` to remove NA rows
ggplot(remove_missing(df, na.rm = TRUE), aes(x, y, colour = Morph)) +
geom_point() +
geom_smooth(aes(linetype = Morph), method = "lm", se = FALSE)
Or alternatively you use na.omit
ggplot(na.omit(df), aes(x, y, colour = Morph)) +
geom_point() +
geom_smooth(aes(linetype = Morph), method = "lm", se = FALSE)
Related
I have a plot that includes data from two different scales. So far, I've plotted both variables and adjusted the scale of one variable (ss) so that it is closer to the other variables. This greatly reduced the white space in the middle of the plot.
set.seed = 42
df <- data.frame(
cat = runif(10, 1, 20),
mean = runif(10, 350, 450),
ss = runif(10, 1, 50))
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
However, I don't love the really long bars for sample size, and I'd like to change the limits on the left y axis so that it starts 250 (where ss = 0). Unfortunately, if I replace my current scale_y_continuous parameter with limits (see below), then the bars disappear. How do I do this?
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(limits = c(250, 510), ### NEW Y AXIS LIMITS
sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
EDIT: Updated plot with #AllanCameron's suggestion. This is really close, but it has the values of the bars extend below 0 on the secondary axis.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510)) ### NEW
Just expand parameter in scale_y_continuous() to c(0,0).
This tells ggplot2 to not add padding to the plot box.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250, name = "sample size"),
expand = c(0,0)) + # New line here!
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510))
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
I wonder how I can get legend category for NA values in scale_fill_brewer. Here is my MWE.
set.seed(12345)
dat <-
data.frame(
Row = rep(x = LETTERS[1:5], times = 10)
, Col = rep(x = LETTERS[1:10], each = 5)
, Y = c(rnorm(n = 48, mean = 500, sd = 1), NA, NA)
)
dat$Y1 <- addNA(cut(log(dat$Y), 5))
levels(dat$Y1)
[1] "(6.21,6.212]" "(6.212,6.214]" "(6.214,6.216]" "(6.216,6.218]" "(6.218,6.22]" NA
library(ggplot2)
ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y1), colour = "white") +
scale_fill_brewer(palette = "PRGn")
You could explicitly treat the missing values as another level of your Y1 factor to get it on your legend.
After cutting the variable as before, you will want to add NA to the levels of the factor. Here I add it as the last level.
dat$Y1 <- cut(log(dat$Y), 5)
levels(dat$Y1) <- c(levels(dat$Y1), "NA")
Then change all the missing values to the character string NA.
dat$Y1[is.na(dat$Y1)] <- "NA"
This makes NA part of the legend in your plot:
I've found a workaround without changing the original data frame, adding an extra legend based on this post:
ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y1), colour = "white") +
scale_fill_brewer(palette = "PRGn")+
geom_point(data = dat, aes(size="NA"), shape =NA, colour = "grey95")+
guides(size=guide_legend("NA", override.aes=list(shape=15, size = 10)))
Colouring the NAs:
ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y1), colour = "white") +
scale_fill_brewer(palette = "PRGn", na.value="red")+
geom_point(data = dat, aes(size="NA"), shape =NA, colour = "red")+
guides(size=guide_legend("NA", override.aes=list(shape=15, size = 10)))
I want to show histograms of multiple groups where the values do not stack. I do this by:
dat <- data.frame(x = seq(-3, 3, length = 20))
dat$y <- dnorm(dat$x)
dat$z <- dnorm(dat$x, mean = 2)
p <- ggplot(dat, aes(x = x)) +
geom_bar(aes(y = y), stat = "identity", alpha = .5, fill = "red") +
geom_bar(aes(y = z), stat = "identity", alpha = .5, fill = "blue")
I'd like to have a fill legend that shows the groupings. I'm not sure why this does not produce any legend (or error):
p + scale_fill_manual(values = c(x = "red", z = "blue"),
limits = c("mean 0", "mean 2")) +
guides(fill=guide_legend(title.position="top"))
Using unnamed values produces the same result.
Thanks,
Max
The legend is automatically generated only if you map fill to variable using aes, like so:
library(reshape2)
ggplot(melt(dat, "x"), aes(x = x)) +
geom_bar(aes(y = value, fill = variable),
stat = "identity", position = "identity", alpha = .5) +
scale_fill_manual(values = c(y = "red", z = "blue"),
labels = c("mean 0", "mean 2"))
I have the following ggplot graph with circles representing the observed data and the crosses the mean for each treatment :
d <- data.frame(Number = rnorm(12,100,20),
Treatment = rep(c("A","B","C", "D"), each = 3))
av <- aggregate(d["Number"], d["Treatment"], mean)
ggplot(data = d, aes(y = Number, x = Treatment)) +
geom_point(shape = 1, size = 6, color = "grey50") +
geom_point(data=av, shape = 4) +
theme_bw()
I would like to add a legend with the exact same symbols on top of the graphs but I'm a bit lost... I use aes to force the creation of legend and then try to modify it with manual scales but the result is not convincing. I would like to have one grey circle of size 6. That sounds also quite complicated for such a basic thing ... There is probably an easyier solution.
ggplot(data = d, aes(y = Number, x = Treatment)) +
geom_point(aes(shape = "1", size = "6", color = "grey50")) +
geom_point(data=av, aes(shape = "4")) +
theme_bw() +
scale_shape_manual(name = "", values = c(1,4), labels = c("observed values", "mean")) +
scale_size_manual(name = "", values = c(6,1), labels = c("observed values", "mean")) +
scale_color_manual(name = "", values = c("grey50","black"),
labels = c("observed values", "mean")) +
theme(legend.position = "top",
legend.key = element_rect(color = NA))
http://imagizer.imageshack.us/v2/320x240q90/842/4pgj.png
The ggplot2 way would be combining everything into a single data.frame like this:
av$Aggregated <- "mean"
d$Aggregated <- "observed value"
d <- rbind(d, av)
ggplot(data = d, aes(y = Number, x = Treatment,
shape=Aggregated, size=Aggregated, colour=Aggregated)) +
geom_point()
And than customize using manual scales and themes.