I don't understand the following: Why does
data_ts <- data.frame(
day = as.Date("2017-06-14") - 0:364,
value = runif(365) + seq(-140, 224)^2 / 10000)
ggplot(data_ts, aes(x=day, y=value)) +
geom_line() +
scale_colour_manual(values = "#ffcc33")
produce a black line? I know, I could use
ggplot(data_ts, aes(x=day, y=value)) +
geom_line(colour = "#ffcc33")
instead, but I'd like to understand why 'scale_colour_manual' does not work in the example above.
The scale_colour_manual function only effects values that are mapped via an aesthetic aes(). Same goes for all scale_* functions. If values aren't set inside the aes(), then the scale won't touch them. If you wanted to use scale_colour_manual, it would need a mapping. Something like
ggplot(data_ts, aes(x=day, y=value)) +
geom_line(aes(color="mycolor")) +
scale_colour_manual(values = "#ffcc33")
or to ensure a correct match up between mapped literal values and colors, you can do something like
ggplot(data_ts, aes(x=day, y=value)) +
geom_line(aes(color="mycolor1")) +
geom_line(aes(y=value+1, color="mycolor2")) +
scale_colour_manual(values = c(mycolor1="#ffcc33", mycolor2="#33ccff"))
Related
I am trying plot this data, and the line graph is correct, but I can't make the legend show up. Any thoughts?
ggplot(data, aes(x=time_months, size=I(1))) +geom_line(aes(y=monthly_net_revenue, color=I("blue"))) +
geom_line(aes(y=cumsum(discounted_monthly_net_revenue), color=I("purple"))) +
geom_line(aes(y=monthly_expenses, color=I("red"))) +
geom_line(aes(y=cumsum(monthly_revenue), color=I("green")))
This will probably work for you
ggplot(data, aes(x=time_months, size=I(1))) +
geom_line(aes(y=monthly_net_revenue, color="blue")) +
geom_line(aes(y=cumsum(discounted_monthly_net_revenue), color="purple")) +
geom_line(aes(y=monthly_expenses, color="red")) +
geom_line(aes(y=cumsum(monthly_revenue), color="green")) +
scale_color_identity(guide = "legend")
The scale_color_identity() uses the values you pass to color= directly as the color rather than treating them like a group name. You don't need I() with this method.
I am trying to make an extremely single heatmap of percentages using ggplot2 which ideally will just be two single thin columns. I tried the following code, believing that the width option in aes would solve the problem.
p_prev_tg <- ggplot(tg_melt, aes(x = variable , y = OTU, fill = value,
width=.3)) + geom_tile() +
scale_fill_gradientn(colours = hm.palette2(10)) +
xlab(NULL) + ylab(NULL) +
theme(axis.text=element_text(size=7))
p_prev_tg
Unfortunately, this returns a plot with lots of empty space as shown. The plot I would like is those two bars side by side, how can I do this in ggplot?
thanks
What about this solution ?
set.seed(1234)
tg_melt <- data.frame(variable=rep(c("Prevalence_T","Prevalence_NT"), each=10),
OTU=rep(paste0("OTU_",1:10),2),
value=rnorm(20))
library(RColorBrewer)
library(ggplot2)
hm.palette2 <- colorRampPalette(rev(brewer.pal(11, 'Spectral')))
p_prev_tg <- ggplot(tg_melt, aes(x = as.numeric(variable), y = OTU, fill = value)) +
geom_tile() +
scale_fill_gradientn(colours = hm.palette2(10)) +
xlab(NULL) + ylab(NULL) +
theme(axis.text=element_text(size=7)) +
scale_x_continuous(breaks=c(1,2),
limits=c(0,3),
labels=levels(tg_melt$variable))+
theme_bw()
p_prev_tg
I am trying to color bars in ggplot but having issues. Can someone explain how to correctly use the fill parameter and the scale_colour parameters?
library(ggplot2)
df<-data.frame(c(80,33,30),c("Too militarized","Just doing their job","Unfairly tarnished by a few"),c("57%","23%","21%"))
colnames(df)<-c("values","names","percentages")
ggplot(df,aes(names,values))+
geom_bar(stat = "identity",position = "dodge",fill=names)+
geom_text(aes(label=percentages), vjust=0)+
ylab("percentage")+
xlab("thought")+
scale_colour_manual(values = rainbow(nrow(df)))
Working barplot example
barplot(c(df$values),names=c("Too militarized","Just doing their job","Unfairly tarnished by a few"),col = rainbow(nrow(df)))
The main issue is that you don't have fill inside a call to aes in geom_bar(). When mapping from data to visuals like colors, it has to be inside aes(). You can fix this by either wrapping fill=names with aes() or by just specifying fill colors directly, instead of using names:
Option 1 (no legend):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", fill=rainbow(nrow(df))) +
ylab("percentage") +
xlab("thought")
Option 2 (legend, because mapping from data to colors):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", aes(fill=names)) +
ylab("percentage") +
xlab("thought") +
scale_fill_manual(values=rainbow(nrow(df)))
Note that in both cases you might want to explicitly factor df$names ahead of the call to ggplot in order to get the bars in the order you want.
I'd like to create a ggplot graph with vertical lines of different colors. Here one way to achieve this goal.
mtcars$colors = rep(1:4, nrow(mtcars)/4)
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_vline(xintercept=subset(mtcars, colors==1)$wt, color="red") +
geom_vline(xintercept=subset(mtcars, colors==2)$wt, color="blue") +
geom_vline(xintercept=subset(mtcars, colors==3)$wt, color="yellow") +
geom_vline(xintercept=subset(mtcars, colors==4)$wt, color="green")
This solution is not very handy when the variable colors takes 50 different values 1) because it asks the user to write a very long expression (or to construct the ggplot object iteratively) and 2) because it does not produce legends for the colors. Is there a better solution?
Maybe this instead:
+ geom_vline(aes(xintercept = wt,color = factor(colors))) +
scale_color_manual(values = c('red','blue','yellow','green'))
I am working on finishing up a graph generated using ggplot2 like so...
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue))
+ geom_point() + stat_smooth() + scale_y_continuous(formatter=comma)
I have attached the result and you can see the numeric values in the legend for Revenue do not have a comma. How can I add a comma to those values? I was able to use scale_y_continuous for the axis, can that be used for the legend also?
Just to keep current, in ggplot2_0.9.3 the working syntax is:
require(scales)
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(labels=comma) +
scale_colour_continuous(labels=comma)
Also see this exchange
Note 2014-07-16: the syntax in this answer has been obsolete for some time. Use metasequoia's answer!
Yep - just a matter of getting the right scale_colour_ layer figured out. Try:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors, colour = Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
I personally would also move my the colour mapping to the geom_point layer, so that it doesn't give you that odd line behind the dot in the legend:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors)) +
geom_point(aes(colour = Revenue)) +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
...as I stumbled over this older thread, maybe it makes sense to add you need to load library("scales"), otherwise you get the following error message
Error in check_breaks_labels(breaks, labels) : object 'comma' not found