ggplot(data = s2[1:39,], aes(x=Month, y=Users, group=Year, col=Year)) + geom_line(size=1.3) + geom_point(size=4.5) + geom_point(size=4.0, color='#FFFFFF') + ggtitle("Seoul_year") + scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8,9,10,11,12)) + scale_y_continuous(labels = scales::comma)
From the upper code I keep getting same blueish colors(per year) in plotting line graph in R.
However, I want to apply different color of lines in the graph.
Which code line should I fix?
try
scale_color_gradient(low="red",high="black")
while red or black can be whatever, including HEX code.
It still means your Year variable is numeric though.
Related
The following code produces a stacked area plot with one line graph just like I intended, however it produces a legend that doesn't match the color of the line graph. How do I fix this? Thanks!
ggplot(data=revenue, aes(x=Date, alpha=0.1)) +
geom_area(aes(y=SENSE.revenues + ORB.revenues + SENSE.refills + ORB.refills,
fill=" ORB Refills"),colour="Black")+
geom_area(aes(y=SENSE.revenues + ORB.revenues + SENSE.refills, fill=" ORB
Revenue"),colour="Black")+
geom_area(aes(y=SENSE.revenues + SENSE.refills, fill=" SENSE
Refills"),colour="Black")+
geom_area(aes(y=SENSE.revenues, fill=" SENSE Revenues"),colour="Black") +
labs(title="Projected Revenues", subtitle="SENSE and ORB",
y="Revenue ($Thousand)") +
scale_fill_discrete(name="Revenue Type") +
theme(axis.text.x = element_text(angle = 75, hjust = 1))+
scale_x_discrete(name ="Date", limits=dates) +
scale_fill_brewer(palette="Purples")+
scale_alpha(guide = 'none')+
theme(legend.title=element_blank())+
geom_line(aes(y=Returns, fill=" Revenues Net of Returns"), colour="Red",
size=0.7)
When you want to plot multiple things in a ggplot graph, it's almost always best in the long run to rearrange your data so you can plot them all at once with an aesthetic distinguishing them, rather than asking for multiple almost-identical geom_s ... here's a rough example.
Make up some data:
set.seed(101)
dd <- data.frame(Date=1:4,
SENSE.revenues=rpois(4,3),
ORB.revenues=rpois(4,3),
SENSE.refills=rpois(4,3),
ORB.refills=rpois(4,3),
Returns=rpois(4,3))
Convert from wide to long:
ddg <- tidyr::gather(dd[,1:5],"type","revenue",-Date)
(using dd[,1:5] leaves off the Returns; if you wanted to commit to tidyverse you could use select(-Returns) here)
Plot with a single geom_area(), distinguished by fill: use position="stack" to get all of the areas plotted one on top of the other ... plot the red line separately, with a faked colour aesthetic (we override the colour value and label with scale_colour_manual()).
ggplot(data=ddg,
aes(x=Date,y=revenue))+
geom_area(position="stack",aes(fill=type))+
scale_fill_brewer(palette="Purples", name="revenue type")+
geom_line(data=dd,aes(y=Returns,colour="junk"))+
scale_colour_manual(values="red",
labels="Returns",
name="")
I'm trying to print an empty ggplot, but still include a legend/scale and I'm running into problems.
Here's the relevant section of the code:
p <- ggplot(data=df, aes(x=factor(year_id), y=location_name)) +
geom_point(aes(size=count.sum), na.rm=F) +
#geom_blank() +
theme_bw() +
theme(axis.text.x=element_text(angle=50, hjust=1)) +
labs(x="Year", y="") +
scale_size(name="Site-years of data", range=c(min(breaks),max(breaks)), breaks=breaks) +
ggtitle(paste0(cause_name, ", Data Availability")) +
facet_wrap(~type)
plots[[i]] <- p
Breaks is just the vector 1,2 and the column count.sum is all NA.
What I want is to print out the chart, which has no data in it (no points), but still have a legend with a small dot representing 1 and a bigger dot representing 2.
As it is right now, I get the error "Discrete value supplied to continuous scale".
If I use geom_blank instead of geom_point, the chart successfully gets created and is empty, but there's no legend,
How can I get both the empty chart and a legend?
Thanks
you could zoom somewhere outside the range of the data, ggplot(data.frame(x=1:10), aes(x,x,size=x)) + geom_point() + coord_cartesian(xlim=c(20,30))
I am trying to change the style settings of this kind of chart and hope you can help me.
R code:
set_theme(theme_bw)
cglac$pred2<-as.factor(cglac$pred)
ggplot(cglac, aes(x=depth, colour=pred2))
+ geom_bar(aes(y=..density..),binwidth=3, alpha=.5, position="stack")
+ geom_density(alpha=.2)
+ xlab("Depth (m)")
+ ylab("Counts & Density")
+ coord_flip()
+ scale_x_reverse()
+ theme_bw()
which produces this graph:
Here some points:
What I want is to have the density line as black and white lines separated by symbols rather than colour (dashed line, dotted line etc).
The other thing is the histogram itself. How do I get rid of the grey background in the bars?
Can I change the bars also to black and white symbol lines (shaded etc)? So that they would match the density lines?
Last but not least I want to add a second x or in this case y axis, because of flip_coord(). The one I see right now is for the density. The other one I need would then be the count data from the pred2 variable.
Thanks for helping.
Best,
Moritz
Have different line types: inside aes(), put linetype = pred2. To make the line color black, inside geom_density, add an argument color = "black".
The "background" of the bars is called "fill". Inside geom_bar, you can set fill = NA for no fill. A more common approach is to fill in the bars with the colors, inside aes() specify fill = pred2. You might consider faceting by your variable, + facet_wrap(~ pred2, nrow = 1) might look very nice.
Shaded bars in ggplot? No, you can't do that easily. See the answers to this question for other options and hacks.
Second y-axis, similar to the shaded symbol lines, the ggplot creator thinks a second y-axis is a terrible design choice, so you can't do it at all easily. Here's a related question, including Hadley's point of view:
I believe plots with separate y scales (not y-scales that are transformations of each other) are fundamentally flawed.
It's definitely worth considering his point of view, and asking yourself if those design choices are really what you want.
Different linetypes for densities
Here's my built-in data version of what you're trying to do:
ggplot(mtcars, aes(x = hp,
linetype = cyl,
group = cyl,
color = cyl)) +
geom_histogram(aes(y=..density.., fill = cyl),
alpha=.5, position="stack") +
geom_density(color = "black") +
coord_flip() +
theme_bw()
And what I think you should do instead. This version uses facets instead of stacking/colors/linetypes. You seem to be aiming for black and white, which isn't a problem at all in this version.
ggplot(mtcars, aes(x = hp,
group = cyl)) +
geom_histogram(aes(y=..density..),
alpha=.5) +
geom_density() +
facet_wrap(~ cyl, nrow = 1) +
coord_flip() +
theme_bw()
I'm having two different problems with specifying the colors in my legends in ggplot. I've tried to make a simplified examples that shows my problem:
df <- data.frame(x=rep(1:9, 10), y=as.vector(t(aaply(1:10, 1, .fun=function(x){x:(x+8)}))), method=factor(rep(1:9, each=10)), DE=factor(rep(1:9, each=10)))
ggplot(df, aes(x, y, color=method, group=DE, linetype=DE)) + geom_smooth(stat="identity")
For some reason, the line types shown in the legend under the title DE are all blue. I'd like them to be black, but I have no idea why they're blue in the first place, so I'm not sure how to change them.
For my other problem, I'm trying to use both point color and point shape to show two different distinctions in my data. I'd like to have legends for both of these. Here's what I have:
classifiers <- c("KNN", "RF", "NB", "LR", "Tree")
des <- c("Uniform", "Gaussian", "KDE")
withoutDE <- c(.735, .710, .706, .628, .614, .720, .713, .532, .523, .557, .677, .641, .398, .507, .538)
withDE <- c(.769, .762, .758, .702, .707, .752, .745, .655, .721, .733, .775, .772, .749, .756, .759)
df <- data.frame(WithoutDE=withoutDE, WithDE=withDE, DE=rep(des, each=5), Classifier=rep(classifiers, 3))
df <- cbind(df, Method=paste(df$DE, df$Classifier, sep=""))
ggplot() + geom_point(data=df, aes(x=WithoutDE, y=WithDE, shape=Classifier, fill=DE), size=3) + ylim(0,1) + xlim(0,1) + xlab("AUC without DE") + ylab("AUC with DE") + scale_shape_manual(values=21:25) + scale_fill_manual(values=c("pink", "blue", "white"), labels=c("Uniform", "KDE", "Gaussian")) + theme(legend.position=c(.85,.3))
If I change the color to change as well as the fill (by putting color=DE into the aes), then those are visible in the legend. I like having the black border around the points, though. I'd just like to have the inside of the points in the legend reflect the point fill in the plot. (I'd also like to position the two legends side-by-side, but I really just want to get the color to work right now)
I've spent way too long googling about both of these problems and trying various solutions without any success. Does anyone have any idea what I'm doing wrong?
For question 1:
Give the legend for line type and the legend for colour the same name.
ggplot(df, aes(x, y, color=method, group=DE, linetype=DE)) +
geom_smooth(stat="identity") +
scale_color_discrete("Line") +
scale_linetype_discrete("Line")
For question 2:
I do not think your fills are matching your data. You should assign the name of the value to each colour in the scale_x_manual calls.
I couldn't get the black border for the points. Here is what I was able to get, though:
ggplot() +
geom_point(data=df, aes(x=WithoutDE, y=WithDE, shape=Classifier,
fill=DE, colour=DE), size=3) +
ylim(0,1) + xlim(0,1) +
xlab("AUC without DE") +
ylab("AUC with DE") +
scale_shape_manual(values=21:25) +
scale_fill_manual(values=c("Uniform"="pink", "KDE"="blue", "Gaussian"="white"),
guide="none") +
scale_colour_manual(values=c("Uniform"="pink", "KDE"="blue", "Gaussian"="white"),
labels=c("Uniform", "KDE", "Gaussian")) +
theme(legend.position=c(.85,.3))
I don't know if you can control the point type inside the legends. Maybe someone else with more knowledge of ggplot2 can figure it out.
I want to plot a path and show where the datapoints are.
Combine Points with lines with ggplot2
uses geom_point() + geom_line() but I do not like that the dots are much thicker and the lines have a discontinuous look - x - x ----- x --- thus I decidet to
create my own dotted line:
mya <- data.frame(a=1:20)
ggplot() +
geom_path(data=mya, aes(x=a, y=a, colour=2, size=1)) +
geom_point(data=mya, aes(x=a, y=a, colour=1, size=1)) +
theme_bw() +
theme(text=element_text(size=11))
I like that the dots and the line have the same size. I did not use the alpha channel because I fear trouble with the alpha channel when I include the files in other programs.
open problems:
R should not create those legends
can R calculate the "darker colour" itself? darker(FF0000) = AA0000
how can I manipulate the linethickness? The size= parameter did not work as expected in R 2.15
Aesthetics can be set or mapped within a ggplot call.
An aesthetic defined within aes(...) is mapped from the data, and a legend created.
An aesthetic may also be set to a single value, by defining it outside aes().
In your case it appears you want to set the size to a single value. You can also use scale_..._manual(values = ..., guide = 'none') to suppress the creation of a legend.
This appears to be what you want with colour.
You can then use named colours such as lightblue and darkblue (see ?colors for more details)
ggplot() +
geom_line(data=mya, aes(x=a, y=a, colour='light'), size = 2) +
geom_point(data=mya, aes(x=a, y=a, colour='dark'), size = 2) +
scale_colour_manual(values = setNames(c('darkblue','lightblue'),
c('dark','light')), guide = 'none') +
theme_bw()