I am actually trying to do a graph with ggplot2 but I'd like to add some options (colors, legend...).
Here is my code :
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment)) +
stat_summary(fun.y="mean", geom="bar") +
facet_grid(. ~ treatment) +
theme_grey() +
xlab("Treatment") +
ylab("OT") +
scale_fill_grey() +
theme(strip.background = element_rect(colour = "black", fill = "white"))
And here the actual output.
Could you please indicate me how to change the name of 1 and 2 (without changing in it the dataframe) and how to add colours to this ?
I tried this
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, colour=Treatment))
But it applies the color only to the outline of the figure.
To change the color of the bars you need fill = Treatment.
To change the labels on the x axis you need scale_x_discrete(labels = your_labels). See here.
So your code will look like:
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, fill= Treatment)) +
scale_x_discrete(labels = your_labels) +
...
Related
legend <- c("score" = "black", "answer" = "red")
plot <- df_l %>% ggplot(aes(date, score, color = "score")) + geom_line() +
geom_vline(aes(xintercept = getDate(df_all %>% filter(name == List[5])), color = "answer"), linetype = "dashed", size = 1,) +
scale_color_manual(name = "Legend", values = legend) +
scale_x_date(labels = date_format("%m/%y"), breaks = date_breaks("months")) +
theme(axis.text.x = element_text(angle=45)) +
labs(title = "", x = "", y = "", colors = "Legend")
I get the result above and could not figure out how to resolve the problem that in the legend always both lines are mixed up. One legend should of course show the slim black line only and the other the dashed black line. Thanks in advance!
The issue you have is that geom_vline results in a legend item that is a vertical line and geom_line gives you a horizontal line item. One solution is to create the legend kind of manually by specifying the color= aesthetic in geom_line... but not in geom_vline. You can then create a kind of "dummy" geom with geom_blank that serves as a holding object for the aesthetics of color=. You can then specify the colors for both of those items via scale_color_manual. Here's an example:
set.seed(12345)
df <- data.frame(x=1:100,y=rnorm(100))
ggplot(df, aes(x,y)) + theme_bw() +
geom_line(aes(color='score')) +
geom_vline(aes(xintercept=4), linetype=2, color='red', show.legend = FALSE) +
geom_blank(aes(color='my line')) +
scale_color_manual(name='Legend', values=c('my line'='red','score'='black'))
That creates the one legend for color... but unfortunately "my line" is solid red, when it should be dashed. To fix that, you just apply the linetype= aesthetic in the same way.
ggplot(df, aes(x,y)) + theme_bw() +
geom_line(aes(color='score', linetype='score')) +
geom_vline(aes(xintercept=4), linetype=2, color='red', show.legend = FALSE) +
geom_blank(aes(color='my line', linetype='my line')) +
scale_linetype_manual(name='Legend', values=c('my line'=2,'score'=1)) +
scale_color_manual(name='Legend', values=c('my line'='red','score'='black'))
I want to change the color of ggplot bar charts manually using the scale_color_manual function. Here is the code:
library(ggplot2)
ggplot(UM.Leads, aes(Leads, Count, fill = Model)) +
geom_bar(stat = "identity") +
xlab("Electrode Model") +
ylab("DBS Leads") +
ggtitle("University of Minnesota") +
scale_color_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()
I cannot seem to change the fill of the bar graphs from the default pink, green, and blue that ggplot provides. Any help would be much appreciated!
See plot here: http://rpubs.com/Gopher16/393415
To illustrate the comment from #Jack Brookes and create a reproducible example:
library(ggplot2)
df <- data.frame(
gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30)
)
ggplot(df, aes(gp, y, fill=gp)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()
I've attempted to manually add a legend to a simple line graph, however after making the changes to insert the legend the actual lines in the graph have become deformed and I don't know what's causing this issue.
Original Code:
ggplot(data=df, aes(x=Time, group=1)) +
geom_line(aes(y = var1), colour = "red") +
geom_line(aes(y = var2), color = "blue") +
geom_line(aes(y = var3), color = "green") +
geom_line(aes(y = var4), colour = "black")
Modified Code for Adding Legend:
df_sub <- df[,c(1, 2,3,4, 5)]
dd = melt(df_sub, id=c("Time"))
ggplot(dd) + geom_line(aes(x=Time, y=value, colour=variable, group = 1 ))+
scale_colour_manual(values=c("red","green","blue", "black"))
Graph:
I would like to colour my boxplot variables differently. I looked here and tried the following but the plot boxes are all the standard white colour (i have 6 factors in Type). What should i change?
library(ggplot2)
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot() +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))
Also you can just change from geom_boxplot() to geom_boxplot(aes(fill=Type)) in you original codes.
ex:
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot(aes(fill=Type)) +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))
What has to change is
geom_boxplot() +
to
geom_boxplot(fill = c("white","white","white","red","blue","white")) +
and remove
scale_fill_manual(values = c("white","white","white","red","blue","white"))
I am using ggplot to plot some data. It works fine but I'd like to control the shape of the plotted area and to remove the grey background.
This is the code I'm using right now:
ggplot(data.melted, aes(x = Year, y = value, colour = variable)) +
geom_line() +
scale_x_continuous("Year") +
scale_y_continuous("Fraction of papers") +
scale_colour_discrete("Topics")
and this is the output it produces:
ggplot(data.melted, aes(x = Year, y = value, colour = variable)) +
geom_line() +
scale_x_continuous("Year") +
scale_y_continuous("Fraction of papers") +
scale_colour_discrete("Topics") + theme(panel.background = element_blank())
To specify dimensions when saving the plot. See ?ggsave for additional options.
ggsave(p1, file = "plot.png", width = 5, height = 5)