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))
Related
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.
I am using below codes
p <- ggplot() +
geom_bar(data=filter(df, variable=="LA"), aes(x=Gen, y=Mean, fill=Leaf),
stat="identity", position="dodge")+
geom_point(data=filter(df, variable=="TT"),aes(x=Gen, y=Mean, colour=Leaf))+
geom_line(data=filter(df, variable=="TT"), aes(x=Gen, y=Mean, group=Leaf))+
ggtitle("G")+xlab("Genotypes")+ylab("Canopy temperature")+
scale_fill_hue(name="", labels=c("Leaf-1", "Leaf-2", "Leaf-3"))+
scale_y_continuous(sec.axis=sec_axis(~./20, name="2nd Y-axis"))+
theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="top")
graph produced from above code
I want graph like that
data
https://docs.google.com/spreadsheets/d/1Fjmg-l0WTL7jhEqwwtC4RXY_9VQV9GOBliFq_3G1f8I/edit#gid=0
From data, I want variable LA to left side and TT from right side
Above part is resolved,
Now, I am trying to put errorbars on the bar graph with below code, it caused an error, can someone have a look for solution?
p + geom_errorbar(aes(ymin=Mean-se, ymax=Mean+se), width=0.5,
position=position_dodge(0.9), colour="black", size=.7)
For this you need to understand that even you have the second Y-Axis, it is just a markup and everything draw on the graph is still base on the main Y-Axis(left one).
So you need to do two things:
Convert anything that should reference to the second Y-Axis to same scale of the one on the left, in this case is the bar scale (LA variables) whose maximum is 15. So you need to divide the value of TT by 20.
Second Axis needs to label correctly so it will be the main Y-Axis multiply by 20.
p <- ggplot() +
geom_bar(data=filter(df, variable=="LA"), aes(x=Gen, y=Mean, fill=Leaf),
stat="identity", position="dodge") +
# values are divided by 20 to be in the same value range of bar graph
geom_point(data=filter(df, variable=="TT"),aes(x=Gen, y=Mean/20, colour=Leaf))+
geom_line(data=filter(df, variable=="TT"), aes(x=Gen, y=Mean/20, group=Leaf))+
ggtitle("G")+xlab("Genotypes")+ylab("Canopy temperature")+
scale_fill_hue(name="", labels=c("Leaf-1", "Leaf-2", "Leaf-3"))+
# second axis is multiply by 20 to reflect the actual value of lines & points
scale_y_continuous(
sec.axis=sec_axis(trans = ~ . * 20, name="2nd Y-axis",
breaks = c(0, 100, 200, 300))) +
theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="top")
For the error par which is very basic here. You will need to adjust the theme and the graph to have a good looking one.
p + geom_errorbar(data = filter(df, variable=="TT"),
aes(x = Gen, y=Mean/20, ymin=(Mean-se)/20,
ymax=(Mean+se)/20), width=0.5,
position=position_dodge(0.9), colour="black", size=.7)
One final note: Please consider reading the error message, understand what it say, reference to the help document of packages, functions in R so you can learn how to do all the code yourself.
I want to overlay two sets of ggplot panels (each panel is a different country) into one, single ggplot panel, without any rescaling of any of the two plots, but ggplot rescales either one or the other.
I have tried using only one ggplot to include both variables, by doing ggplot(df, aes(x=t, y=a)), and, within that ggplot, then using geom_point and geom_smooth for the second variable (y=b), but this rescales variable a.
# plot 1
g <-ggplot(df, aes(x=year, y=a))
p <-g + geom_point(alpha=0.7) + geom_smooth(method="auto") + facet_wrap(~country, scales="free") + theme_bw() +
xlab("Year") + ylab(bquote('a')) +
scale_x_continuous(breaks=seq(1960, 2020, 15))
# plot 2
a <-ggplot(df, aes(x=year, y=b))
b <-a + geom_point(alpha=0.7, color="green") + geom_smooth(method="auto", color="darkgreen") +
facet_wrap(~country, scales="free") + theme_bw() +
xlab("Year") + ylab(bquote('b')) +
scale_x_continuous(breaks=seq(1960, 2020, 15))
I expect to be able to overlay these two ggplots into a single set of panels, with both y-axes appearing exactly as they appear when they're plotted alone (including units). I would then need to somehow make one of the y-axis appear to the right of the panels, so I have two y-axes, one at each side.
Image 1. ggplot rescales left y-axis. I don't want this to happen.
Image 2. What I want instead is to be able to somehow merge each of these images to get a single panel per country, displaying both the green and the blue lines with the scales that appear here.
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()
I am trying to change the colours on a ggplot column chart. After googling, I thought that the following code would work:
require(ggplot2)
require(RColorBrewer)
State <- c(rep("NSWTC",5), rep("TCV",5), rep("QTC",5),
rep("WATC",5), rep("SAFA",5), rep("Other",5))
Year <- rep(c("11-12","12-13","13-14","14-15","15-16"),6)
##some random data
Funding.Programme <- abs(rnorm(30))
df <- data.frame(State, Year, Funding.Programme)
##this line makes the graph in the order you inputted it, rather than alphabetical
df$State <- factor(df$State, levels=unique(df$State))
##ugly coloured
bars <- ggplot(df) +
aes(x=Year ,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements')
##nicely coloured
blues <- brewer.pal(5, "Blues")
blues <- rev(blues)
##the following two graphs have the same colours
bars <- ggplot(df) +
aes(x=Year ,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements') +
scale_fill_brewer(blues)
bars
bars <- ggplot(df) +
aes(x=Year ,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements') +
scale_fill_brewer(blues.rev)
bars
##and this does not adjust the default colours
bars <- ggplot(df)+
aes(x=Year,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements') +
scale_colour_manual(values = blues.rev)
bars
But the last method does not work, and the second and third-last charts produced are identical, despite the order of colours being reversed in the object.
You want scale_fill_manual(values = blues) or conversely with blues.rev (which you didn't actually create in your example code, which I assume is a typo).
Only use scale_*_brewer when you're selecting one of the default palette's by name. Otherwise, use scale_*_manual for this sort of thing.
The last one doesn't work because you were using colour instead of fill.
Finally, carriage returns and tabs: love them, cherish them, use them!