I am trying have a multicolor bar chart, but scale_fill_manual or scale_color_brewer etc. do neither work outside or inside geom_bar -> Can sb help, please?
ggplot(data=sust_future, aes(x=as.factor(`sust.self-perception`))) +
geom_bar(aes(y = (..count..)/sum(..count..)),width=0.6) +
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) +
scale_y_continuous(labels=scales::percent) +
labs(title = "xxx", x = "", y = "") +
theme(legend.position = "bottom",
axis.text.x=element_text(size=8),
axis.text.y=element_text(size=8))
I'm not sure how you want the bars to be coloured (for example based on a factor in a column or based on some other conditions). If you are trying to colour your geom_bar based on something in your data, you'd have to define how you'd like to colour in your bars via aes(fill = column_name) either globally in ggplot() or specifically in geom_bar(). Here is an example you can try to implement in your case (as I do not know what is in sust_future:
# global setting
ggplot(iris, aes(x = Species, fill = Species)) +
geom_bar()+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# local to geom_bar
ggplot(iris, aes(x = Species)) +
geom_bar(aes(fill = Species))+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
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'))
Below is my code to plot Stacked BarPlot
ggplot(data = mdata, aes(x = variable, y = value, fill = Species)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.text=element_text(size=rel(0.7)),
legend.key.size = unit(0.5, "cm")) +
scale_y_continuous(labels=function(x)x*100) +
coord_flip() +
ylab("Species Percentage") +
xlab("Samples")
OutputPlot:
As you can see from the plot my Species legends are split in to 5 column list, which takes the 50% of the total plot layout.
Is there a way to make/convert legend list in to only 2 or 3 column so that area above and below will be covered and BarPlot can be widened.
Also to make Legend Text Bold its looking blurred with many legends
You can set any number of columns with the ncol argument in guide_legend():
library(ggplot2)
dat <- cbind(car = rownames(mtcars), mtcars)
ggplot(dat, aes(mpg, wt, colour = car)) +
geom_point() +
scale_colour_discrete(guide = guide_legend(ncol = 3))
EDIT: As Z.Lin pointed out, for fill scales; replace scale_colour_* by scale_fill_*.
This question already has answers here:
How to set multiple legends / scales for the same aesthetic in ggplot2?
(2 answers)
Closed 8 months ago.
This code obviously does not work (it uses the same legend and colour scheme for both categorical variables.
require(ggplot2)
dt <- ggplot2::diamonds ; dt <- dt [1:20,];dt
ggplot(dt) +
geom_point(aes(depth,carat, col=cut)) +
geom_point(aes(depth,carat, col=clarity)) +
scale_colour_brewer("greens", name="cut") +
scale_colour_brewer("Reds", name="cut") +
guides(colour= guide_legend("CUT")) +
guides(colour = guide_legend("CLARITY"))
What's the correct way to plot this?
There is no correct way to do this. Ggplot isn't intended to be used this way since you are trying to map two variables to the same scale. However, you could circumvent the limitations of ggplot to some extend by hijacking the fill scale to do the job for you:
ggplot(dt) +
geom_point(aes(depth, carat, fill = cut), shape = 21, colour = "transparent") +
geom_point(aes(depth, carat, colour = clarity)) +
scale_colour_brewer(palette = "Greens", name = "cut") +
scale_fill_brewer(palette = "Reds", name = "clarity")
The trick is to use a shape that has a fill and use that fill to map your variable. The downside is that this trick can't be extended to any number of variables. There are a few packages out there that can achieve what you want, namely ggnewscale or relayer.
An example with the ggnewscale package:
library(ggnewscale)
ggplot(dt) +
geom_point(aes(depth, carat, colour = cut)) +
scale_colour_brewer(palette = "Greens", name = "cut") +
new_scale_color() +
geom_point(aes(depth, carat, colour = clarity)) +
scale_colour_brewer(palette = "Reds", name = "clarity")
For the relayer variant:
library(relayer)
ggplot(dt) +
rename_geom_aes(geom_point(aes(depth, carat, cut = cut)), new_aes = c("colour" = "cut")) +
rename_geom_aes(geom_point(aes(depth, carat, clarity = clarity)), new_aes = c("colour" = "clarity")) +
scale_colour_brewer(palette = "Greens", aesthetics = "cut") +
scale_colour_brewer(palette = "Reds", aesthetics = "clarity")
Warning: Ignoring unknown aesthetics: cut
Warning: Ignoring unknown aesthetics: clarity
Hope this helped!
EDIT: obviously on the plots above only one of the colours show on the points because you are overplotting the same x and y coordinates on top of each other. I felt like I needed to point this out.
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 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) +
...