I am generating a bar chart from data that is in the following format:
count | month
------|-----------
1000 | 2012-01-01
10000 | 2012-02-01
I am trying to assign continuous colors to my bar chart. I want a gradient of colors to be used as the fill color of the bars.
I am trying this:
ggplot(userData, aes(month, count)) +
geom_bar(stat = "identity") +
scale_x_date() +
# scale_fill_gradient(low="blue", high="red") +
scale_fill_continuous(low="blue", high="red", limits=c(0,6500000)) +
labs(x= "Time", y="Count")
However, I am not getting the desired result and the bars in the chart still stays gray as can be seen below:
I have tried with both scale_fill_continuous, and scale_fill_gradient, but without success.
I am not sure exactly what I am missing here.
You haven't assigned the fill aesthetic to anything: aes(month, count, fill = count) should do the trick.
Complete code:
ggplot(userData, aes(month, count, fill = count)) +
geom_bar(stat = "identity") +
scale_x_date() +
scale_fill_continuous(low="blue", high="red") +
labs(x= "Time", y="Count")
(limits are probably useless now, but feel free to add them back)
Related
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"))
As we can see in this graph (full graph here):
There are two variables, the lighter orange bar shows the total value and another one is showing the partial variable. How can I organize those two bars together? (not 'stack', because I want both bars to start from the bottom)
ggplot(bad_drivers2, aes(State, nums)) +
geom_bar(stat = "identity", fill= "royalblue") +
coord_flip()
ggplot(bad_drivers2, aes(State, not_distracted_num)) +
geom_bar(stat = "identity", fill= "blue") +
coord_flip()
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) +
...
I'm trying to make funnel chart in R. As an example I took code from this page https://analyzecore.com/2015/06/23/sales-funnel-visualization-with-r/
But the strange thing is, which I could not get, why my bars are skewed to the left side?
Funnel data:
Category Number
1 total_members 1000
2 paid_members 936
3 cancellations 452
4 refunds 34
library(ggplot2)
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols)+
geom_bar(data=funnel_data, aes(x=Category, y=Number, fill=Category), stat="identity", width=1)
Results in
If you just run the piece of code from the article, this one for example:
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols) +
geom_bar(data=df.all, aes(x=step, y=number, fill=content), stat="identity", width=1)
It will give you a nice example with bars centered by X:
I have no clue what is the issue in this case. Would be very glad for any assisance.
This is different approach, but works - create second geom_bar geom with negative values:
library(ggplot2)
# Using OPs data
ggplot(funnel_data, aes(Category, fill = Category)) +
geom_bar(aes(y = Number), stat = "identity", width = 1) +
geom_bar(aes(y = -Number), stat = "identity", width = 1) +
theme_minimal() +
coord_flip()
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"))