I want to create a line+dots plot with confidence intervals all in grey, but I obtain a coloured plot.
I used the option scale_fill_grey but this worked only on the confidence intervals and not on lines and points.
a<-ggplot(df, aes(x = Yr, y = SIR, color=Type, shape=Type,linetype=Type))+geom_point(size=2.5) + geom_smooth(method=lm, aes(fill=Type))
a + scale_fill_grey(start=0.8, end=0.5)+labs(x="Year", y="SIR")+theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
How can I obtain a BW-grey plot?
Thank you!
To change the colour of your points and lines, you need to use the colour aesthetic and not fill.
Creating a similar plot with the iris dataset, and adding scale_fill_grey() will only colour the confidence areas of your plot:
a<-ggplot(iris, aes(x = Petal.Length, y = Sepal.Length, color=Species, shape=Species,linetype=Species))+geom_point(size=2.5) + geom_smooth(method=lm, aes(fill=Species))
a + scale_fill_grey()
To get the points as well, you need to add scale_color_grey():
a + scale_fill_grey() + scale_color_grey()
For more details on ggplot colour aesthetics, see the docs:
https://ggplot2.tidyverse.org/reference/aes_colour_fill_alpha.html
Related
I want to add a legend it shows black for the 7 day moving average and blue for the bars(daily cases). so it looks something similar to the NHS graph , but the legend does not work when i add it into my code?
ggplot(LimerickNew1, aes(x=TimeStamp, y=DailyCCase,Shape=MA7)) +
geom_bar(stat="identity",fill="steelblue") +
geom_line(aes(y=MA7),size=1.5) +
labs( x="", y="Total cases", title="Total Covid Cases for County Limerick 01-03-20 to 01-
06-`20" )+`
theme_bw()+
theme(legend.background = element_rect(fill = "darkgray"),
legend.key = element_rect(fill = "lightblue", color = NA),
legend.key.size = unit(1.5, "cm"),
legend.key.width = unit(0.5,"cm"),
axis.text.x = element_text(face="bold", color="#008000",size=12, angle=0),
axis.text.y = element_text(face="bold", color="#008000",size=12, angle=0),
axis.title.x = element_text(face="bold", size=12, color="black"),
plot.title = element_text( face = "bold", colour = "black", size = 20,, hjust = 0.5))
Without a reproducible example, it's hard to give a more specific answer for your question, but here's a method that expands upon the comment from #teunbrand and should guide you toward showing a legend.
TL;DR - The reason you're not seeing a legend is because you do not have any layers or geoms drawn that have one of the aesthetics mapped to aes(). All aesthetics like color, size, and fill are specified for each geom outside of an aes() function, which means there's no reason for ggplot2 to draw a legend. To get a legend to be drawn, you need to specify the aesthetic inside aes().
Example
Here's an illustrative example that is similar to OP's question.
library(ggplot2)
set.seed(8675309)
df <- data.frame(
x = 1:100,
y = rnorm(100, 10))
df$z <- log(cumsum(df$y))
ggplot(df, aes(x=x)) +
geom_col(aes(y=y), fill='blue', alpha=0.3) +
geom_line(aes(y=z), size=2) +
ylim(0,25) + theme_bw()
Showing a Legend
To get a legend to show, you have to specify one or more aesthetics inside aes(). The fun fact here is that you don't have to specify a column in the dataset when doing this. If you specify a single value, it will apply to the whole dataset, and basically just show a legend key for the entire set of observations. This is what we want.
ggplot(df, aes(x=x)) +
geom_col(aes(y=y, fill="the columns"), alpha=0.3) +
geom_line(aes(y=z, size="the line"), color="black") +
ylim(0,25) + theme_bw()
Formatting
The names come from the values specified in aes(), and the color change is due to default mapping of color values. To get things to look "right", you want to use some theme() elements to:
keep only one name
squish legends together
position on the chart, rather than on the side
The end result is here:
ggplot(df, aes(x=x)) +
geom_col(aes(y=y, fill="the columns"), alpha=0.3) +
geom_line(aes(y=z, size="the line"), color="black") +
ylim(0,25) +
scale_fill_manual(values="blue") + # recolor the columns
guides(
fill=guide_legend(title="My Legend", order=1),
size=guide_legend(title=NULL, order=2)
) +
theme_bw() +
# legend placement and spacing
theme(
legend.title = element_text(margin=margin(b=10)),
legend.spacing.y = unit(-3,'pt'),
legend.position = c(0.8, 0.8)
)
I'm struggling to plot a gradient color scale for each facet in facet_wrap() independently.
Data is too big to post it here but here is my code:
ggplot(stack, aes(hour, day)) +
geom_tile(aes(fill = percent), colour = "white") +
facet_wrap(~author, ncol = 3) +
theme_minimal() +
scale_fill_distiller(palette = 'RdYlBu') +
theme(
axis.title.x = element_blank(), axis.title.y = element_blank(),
legend.position = "none",
strip.background = element_rect(fill = '#E5E6E1'),
strip.text = element_text(face = 'bold')
)
However, if I plot individually only one author, I get:
I just wanna plot each facet with its own gradient color scale, not share with the rest of the facets. Should be very simple, but I don't manage to do it. I tried adding group = author within aes() in geom_tile() and ggplot() but it wouldn't work.
After much research, I ended up using the solution provided here that uses gridExtra. I guess there is no easy way to do it using only ggplot.
I've tried to create an alpha plot but I couldn't find right way to do it. I tried different combination to figure out and I've almost there but I need a little help.
My question is how can I get rid off blue color in the plot.
My script is `
p <- ggplot(df, aes(x=x, y=y))
p + geom_hex(aes(alpha=..count..),bins=20)+
scale_x_log10("ratio following/followers",
labels = trans_format("log10", math_format(10^.x))) +
scale_y_log10("ratio messages received/sent",
labels = trans_format("log10", math_format(10^.x))) +
theme_bw() +
theme(panel.background = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor=element_blank(),
plot.background = element_blank())+
#guides(fill=FALSE)+
scale_alpha_continuous ("Counts",breaks=c(0,2000,4000,6000,8000,10000))+
geom_vline(xintercept =1, color="red", size=0.25, linetype=5)+
geom_hline(yintercept =1, color="red", size=0.25, linetype=5) +
annotate('text', x=500, y=0.01, size=3, label="4\ncommon\nusers") +
annotate('text', x=0.0001, y=0.01, size=3, label="3\nbroadcasters") +
annotate('text', x=0.0001, y=7000, size=3, label="1\ninfluentials") +
annotate('text', x=500, y=7000, size=3, label="2\nhidden\ninfluentials")
This script creates this plot
I can be able to get rid off blue legend with activating "guides(fill=FALSE)+" line in the script and it give this:
You can reach sample data from here
Thanks #Didzis Elferts for his answer. I couldn't be sure about the legend and plot breaks colors. As you can see these pictures 10K and 8K has the same color (Am I right!) so 10K should be darker, shouldn't be.
Function geom_hex() by default maps counts to the fill and so you get fill gradient (blue by default). If you just want map counts to alpha values then you must assign fill = outside the aes() of geom_hex() to some color (used grey45 as example). As fill = is set outside the aes() there won't be legend for fill values.
p + geom_hex(aes(alpha=..count..),bins=20, fill = "grey45")+ ...
I am trying to draw this following graph using ggplot2 package, but somehow the axis won't show up. the ticks are there, just not the axis line. I have used the theme(axis.line=element_line()) function, but it wouldn't work.
Here is my code:
library(ggplot2)
ggplot(data = soepl_randsub, aes(x = year, y =satisf_org, group = id)) +
geom_point() + geom_line() +ylab("Current Life Satisfaction") +theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() ) +
theme(panel.border= element_blank()) +
theme(axis.line = element_line(color="black", size = "2"))
I am not sure what went wrong. Here is the chart.
The bug was fixed in ggplot2 v2.2.0 There is no longer a need to specify axis lines separately.
I think this is a bug in ggplot2 v2.1.0. (See this bug report and this one.) A workaround is to set the x-axis and y-axis lines separately.
library(ggplot2)
ggplot(data = mpg, aes(x = hwy, y = displ)) +
geom_point() +
theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )+
theme(panel.border= element_blank())+
theme(axis.line.x = element_line(color="black", size = 2),
axis.line.y = element_line(color="black", size = 2))
You don't need to specify axis-size for X and Y separately. When you are specifying size="2", R is considering value 2 as non-numeric argument. Hence, axis-line parameter is defaulted to 0 size. Use this line of code:
ggplot(data = mpg, aes(x = hwy, y = displ)) + geom_point() +xlab("Date")+ylab("Value of Home")+theme_bw() +theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + theme(panel.border= element_blank()) +
theme(axis.line = element_line(color="black", size = 2))
axis_line inherits from line in R, hence specifying size is mandatory for non-default values.
I'm trying to replicate the following chart using ggplot2
The one change I'd like to make from that chart though is to give a colour to each point and its label. Here's what I've tried so far:
library(ggplot2)
library(directlabels)
Z <- c("Label1", "Label2", "Label3", "Label4", "Label5", "Label6", "Label7",
"Label8", "Label9", "Label10", "Label11", "Label12", "Label13", "Label14",
"Label15", "Label16", "Label17", "Label18", "Label19", "Label20", "Label21",
"Label22", "Label23", "Label24")
X <- c(10.32582421, 9.772686421, -13.99202201, 3.803952545, 7.775395482,
-11.82234956, -24.27906403, -6.864457678, -24.62853773, 15.3562638,
-6.476057462, 9.576414602, -5.504090215, 29.74512913, 9.046116821,
15.79954557, -39.61679645, -0.90307239, 21.12503086, 15.30221473,
13.40781808, -6.803226537, -4.045907666, -0.134057007)
Y <- c(0.037608141, 0.010581738, 0.117730985, 0.022347258, 0.069347278,
0.026699666, 0.028739498, 0.040611306, 0.036626248, 0.034854158,
0.039310836, 0.03122964, 0.009422296, 0.021935924, 0.050006846,
0.036285691, 0.016796701, 0.057764277, 0.028421772, 0.042726693,
0.037513217, 0.058422072, 0.066859355, 0.078158403)
mychart <- data.frame(Z, X, Y)
q <- ggplot(mychart, aes(X, Y)) + geom_point(aes(colour = Z)) + theme_bw()
direct.label(q)
And I get the following result:
There are three things I'm having trouble figure out:
I'd like to remove the grey quadrant lines.
I'd like to move the axes so that they are centered in the chart, with plots distributed across the 4 quadrants.
I'd like to reduce the label font sizes - I suspect that's why some of them don't end up close to their points.
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
Use +xlim(min,max) and +ylim(min,max) to set the axis limits for your plot. Then you can use +geom_hline(y=yvalue) and +geom_vline(x=xvalue) to add the horizontal and vertical lines to your plot to designate the four quadrants.
Instead of using +direct.label(q), use +geom_text(aes(label=q,size=sizevalue),where 'sizevalue' is a numeric value that determines the size of the labels (so you can experiment with this).
EDIT: Try this code, which should fix your point labels. (I don't know of a way to move the axis labels up to the lines you drew in, nor a native way to simply move the original axes into the center of your plot. Sorry!):
ggplot(mychart, aes(X, Y)) +
geom_point(aes(colour = Z)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black")) +
xlim(-40,40) +
ylim(0,0.12) +
geom_hline(y=0.04) +
geom_vline(y=0) +
geom_text(aes(x=X,y=Y+0.003,label=Z,color=Z)) +
theme(legend.position="none")
EDIT 2: Jitter in geom_text
ggplot(mychart, aes(X, Y, colour=Z)) +
geom_point() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black")) +
geom_text(aes(label=Z),
position = position_jitter(width=2, height=0.005)) +
xlim(-40,40) +
ylim(0,0.12) +
geom_hline(y=0.04) +
geom_vline(y=0) +
theme(legend.position="none")