I'm trying to adjust the point and line size independently in my legend. I'm wanting to be able to discern between the dashed and solid line while also having my point shapes be distinctive enough overtop of the line in the legend. Right now, I can't seem to figure out how to make the line smaller - I've figured out how to adjust the point size though. Any help/thoughts are all appreciated; definitely still a newbie. Thanks!
I'll post an image and code below:
Image of figure with points enhanced, but still can't get line to size correctly in the legend
- Chase
ggplot(df, aes(x = Psych.Distress.Sum, y = Likelihood.Max, color = Feedback)) +
geom_smooth(method = "lm", se = T, aes(linetype = Feedback)) +
geom_jitter(aes(shape = Feedback)) +
labs(x = "Psychological Distress", y = "Endorsement of Max's Feedback Strategy") +
apatheme +
theme(axis.title = element_text(face="bold")) +
theme(legend.text = element_text(face="bold")) +
theme(legend.position = c(0.87, 0.13)) +
scale_color_grey(end = .5) +
theme(legend.key.height= unit(.5, 'cm'),
legend.key.width= unit(1, 'cm')) +
guides(colour = guide_legend(override.aes = list(size= 3, linetype=0)))
This is tricky. Here's a hacky way using two plots that are overlaid on top of each other using patchwork. To prove that the data are aligned, I made the 2nd plot's text be semi-transparent red, but we could make it totally transparent with color #FF000000. This method is a little brittle, since the plots will come out of alignment if they have different ranges or different formats. But if we adjust for that, they line up perfectly with no extra fuss.
Your question didn't include any sample data so I used the mtcars data set.
library(patchwork)
library(ggplot2)
# This layer has the `geom_smooth` and black axis text
(a <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(am))) +
geom_smooth(method = "lm", se = T, aes(linetype = as.factor(am))) +
scale_color_grey(end = .5) +
guides(linetype = guide_legend(override.aes = list(size = 1.2))) +
labs(x = "Psychological Distress",
y = "Endorsement of Max's Feedback Strategy",
linetype = "Line legend", color = "Line legend") +
coord_cartesian(ylim = c(10, 35)) +
theme_classic() +
theme(axis.title = element_text(face="bold")) +
theme(legend.text = element_text(face="bold")) +
theme(legend.position = c(0.7, 0.8)))
# This layer has the `geom_jitter` and red semi-clear axis text
(b <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(am))) +
geom_jitter(aes(shape = as.factor(am))) +
scale_color_grey(end = .5) +
guides(shape = guide_legend(override.aes = list(size = 3))) +
coord_cartesian(ylim = c(10, 35)) +
labs(x = "", y = "",
color = "Point legend", shape = "Point legend") +
theme_classic() +
theme(plot.background = element_blank(),
panel.background = element_blank(),
axis.text = element_text(color = "#FF000055")) +
theme(legend.position = c(0.7, 0.55)))
a + inset_element(b, 0, 0, 1, 1, align_to = "full")
I'm trying to create a more aesthetically pleasing graph. For some reason I am producing 2 legends at the top of my chart. I would like only the one on the right to remain, but I cannot figure out what I am doing wrong. Here is my code and graph.
plt <- ggplot(ac_total_melt, aes(Date, Value, fill = Action)) +
geom_line(
aes(color = Action),
size = .9
)+
geom_point(aes(colour = Action))+
ylab("")+
scale_color_manual(values=c('Dark Green','Dark red'), labels = c("Number Closed", "Number Opened"))+
geom_text_repel(
aes(color = Action, label = Value),
show.legend = FALSE,
family = "Lato",
fontface = "plain",
size = 4,
direction = "y"
) +
theme(legend.position="top")+
guides(color = guide_legend (override.aes = list(linetype = 0, size=5)))
plt
Data:
Looks like I needed to add show_guide = F to geom_point
The issue is that you re-labeled the fill scale, but not the color scale, so ggplot isn't able to merge them:
plt <- ggplot(ac_total_melt, aes(Date, Value, fill = Action, color = Action)) +
geom_line(size = .9) +
geom_point() +
ylab("") +
scale_color_manual(
values=c('blue', 'green'),
labels = c("Number Closed", "Number Opened"),
aesthetics = c("color", "fill") # apply this scale to both color and fill
) +
geom_text_repel(
aes(label = Value),
show.legend = FALSE,
family = "Lato",
fontface = "plain",
size = 4,
direction = "y"
) +
theme(legend.position = "top")+
guides(color = guide_legend(override.aes = list(linetype = 0, size = 5)))
plt
Where and how do I specify colors, axis lines, and removal of background in geombar? Ultimately, I want to have one bar to be dark gray and one bar to be light gray. They are currently blue and pink which were defaults. I also want the the x and y to have axis lines, and the figure to have no gray background. I have everything else figured out, using the below code. Thank you for your help.
library(ggplot2)
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = myData$mean + myData$se,
ymin = myData$mean - myData$se)
p <- ggplot(data = myData, aes(x = names, y = mean, fill = names)) +
p + geom_bar(stat = "identity", position = dodge) +
geom_errorbar(limits, position = dodge, width = 0.9) +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank(),
axis.title.x=element_blank())
limits <- aes(ymax = myData$mean + myData$se,
ymin = myData$mean - myData$se)
p <- ggplot(data = myData, aes(x = factor(site), y = mean,
fill = factor(infectionstatus)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
geom_errorbar(limits, position = position_dodge(0.9),
width = 0.25) +
labs(x = "Sites", y = "Average Calories in White Muscle Tissue") +
scale_fill_discrete(name = "Infection Status")
You probably wanted something like this:
# Generate data
myData <- data.frame(names = letters[1:2],
mean = 1:2,
SE = 0.1)
# Plot data
library(ggplot2)
ggplot(myData, aes(names, mean)) +
geom_bar(aes(fill = names),
stat = "identity", position = "dodge") +
geom_errorbar(aes(ymin = mean - SE, ymax = mean + SE),
position = position_dodge(width = 0.5), width = 0.5) +
labs(title = "Calorie Amount",
subtitle = "Averaged per Tissue",
x = NULL,
y = "Average Calories in White Muscle Tissue",
fill = "Infection Status") +
scale_fill_manual(values = c("grey40", "grey60")) +
theme_classic() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank())
I used theme_classic() as it does most of the job when you want clean plot. And specified colors with scale_fill_manual(values = c("grey40", "grey60"))
Let's say I have this data.frame:
my.df <- data.frame(mean = c(0.045729661,0.030416531,0.043202944,0.025600973,0.040526913,0.046167044,0.029352414,0.021477789,0.027580529,0.017614864,0.020324659,0.027547972,0.0268722,0.030804717,0.021502093,0.008342398,0.02295506,0.022386184,0.030849534,0.017291356,0.030957321,0.01871551,0.016945678,0.014143042,0.026686185,0.020877973,0.028612298,0.013227244,0.010710895,0.024460647,0.03704981,0.019832982,0.031858501,0.022194059,0.030575241,0.024632496,0.040815748,0.025595652,0.023839083,0.026474704,0.033000706,0.044125751,0.02714219,0.025724641,0.020767752,0.026480009,0.016794441,0.00709195), std.dev = c(0.007455271,0.006120299,0.008243454,0.005552582,0.006871527,0.008920899,0.007137174,0.00582671,0.007439398,0.005265133,0.006180637,0.008312494,0.006628951,0.005956211,0.008532386,0.00613411,0.005741645,0.005876588,0.006640122,0.005339993,0.008842722,0.006246828,0.005532832,0.005594483,0.007268493,0.006634795,0.008287031,0.00588119,0.004479003,0.006333063,0.00803285,0.006226441,0.009681048,0.006457784,0.006045368,0.006293256,0.008062195,0.00857954,0.008160441,0.006830088,0.008095485,0.006665062,0.007437581,0.008599525,0.008242957,0.006379928,0.007168385,0.004643819), parent.origin = c("paternal","paternal","paternal","paternal","paternal","paternal","maternal","maternal","maternal","maternal","maternal","maternal","paternal","paternal","paternal","paternal","paternal","paternal","maternal","maternal","maternal","maternal","maternal","maternal","maternal","maternal","maternal","maternal","maternal","maternal","paternal","paternal","paternal","paternal","paternal","paternal","maternal","maternal","maternal","maternal","maternal","maternal","paternal","paternal","paternal","paternal","paternal","paternal"), group = c("F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:M","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1r:F","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:M","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F","F1i:F"), replicate = c(1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6))
Which I plot using ggplot2 as follows:
library(ggplot2)
p1 <- ggplot(data = my.df, aes(factor(replicate), color = factor(parent.origin)))
p1 <- p1 + geom_boxplot(aes(fill = factor(parent.origin),lower = mean - std.dev, upper = mean + std.dev, middle = mean, ymin = mean - 3*std.dev, ymax = mean + 3*std.dev), position = position_dodge(width = 0), width = 0.5, alpha = 0.5, stat="identity") + facet_wrap(~group, ncol = 4)+scale_fill_manual(values = c("red","blue"),labels = c("maternal","paternal"),name = "parental allele")+scale_colour_manual(values = c("red","blue"),labels = c("maternal","paternal"),name = "parental allele")
p1 <- p1 + theme_bw() + theme(plot.background = element_rect(fill = 'white', colour = 'white'), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position = "none") + theme(strip.background=element_rect(fill="white"))
which gives this plot:
My question is whether it is possible to specify the color of the box midlines (but only the midlines) in both the plot and the legend (say to be black).
Workaround could be to use geom_errorbar() and set the ymin and ymax values to mean. Then change color to black. With argument show_guide=TRUE you will get black lines also in legend. Playing with width= argument allows to get width of lines the same as for boxes.
+ geom_errorbar(aes(ymin=mean,ymax=mean,group=factor(parent.origin)),
color="black",width=0.15,show_guide=TRUE)