Using the following code generates this plot
ggplot(df_44, aes(x = latest_tli_date, y = farmer_created_at, fill = ward_id)) +
geom_point()+ scale_color_brewer(palette = "BuPu")
I need the 4 wards to have different colours. What am I doing wrong?
try using the fill argument in the geom_point function
Edit: I just tried an example code, the fill and col arguments is going to color it with on a continuous scale because the data is continuous.
If you factor your value you want to color, it should color it discretely.
ggplot(df_44, aes(x = latest_tli_date, y = farmer_created_at)) +
geom_point(aes(col = factor(ward_id))
Use colour aesthetics:
If ward_id is numeric then you might want to replace by colour = factor(ward_id):
Have a look here: https://ggplot2.tidyverse.org/reference/scale_brewer.html
library(ggplot2)
ggplot(df_44, aes(x = latest_tli_date, y = farmer_created_at, colour = ward_id)) +
geom_point()+ scale_color_brewer(palette = "BuPu")
Related
I'm trying to use ggplot2 to make some sort of timeline using values from a dataframe (df). I've managed to plot the data exactly how I want it (the different colored line segments connecting the x-marks in this exact order, i.e., from left to right: 'early', 'unknown', 'late', 'sub'). The startpoint and endpoint columns in the dataframe are used to define the positions of the points and line segments.
The problem is that the legend doesn't show the color of the 'x' icons, they are just grey. I've tried adding scale_color_manual() and scale_fill_manual() commands but they don't seem to change anything. The legend does display the correct color when I change the shape to shape = 21, however, I really want the shape to be 4 (x icons). I don't care about the shape of the legend though but scale_shape_manual() again didn't change anything about the legend.
I have also tried placing different color arguments inside and outside the aes() argument of ggplot(), geom_segment() and/or geom_point().
How can I make the icons from the legend show the correct color?
Below I added a piece of code to reproduce the problem.
library(ggplot2)
library(RColorBrewer)
## Define dataframe
df <- data.frame(Var = c("sub","late","unknown","early"),
Time = c(10,267,0,1256),
Endpoint = c(1533,1523,1256,1256),
Startpoint = c(1523,1256,1256,0))
colorscheme <- RColorBrewer::brewer.pal(9, "Set1")[c(1,4,2,3)]
## Make plot
ggplot(df, aes(x="", y=Endpoint, fill=Var), color =colorscheme) +
geom_segment( aes(x="", xend="", y=Startpoint, yend=Endpoint), color = colorscheme) +
geom_point(aes(x="", y=Endpoint),size=5, shape=4 , color = colorscheme) +
coord_flip()
Thanks in advance for any suggestions!
You should use color instead of fill. To remove the line from the legend, use guides(color = guide_legend(override.aes = list(linetype = 0))) or use show.legend = F in geom_segment.
Also, arguments passed in ggplot need not to be repeated afterward.
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme) +
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint)) +
geom_point(size=5, shape=4) +
coord_flip() +
guides(color = guide_legend(override.aes = list(linetype = 0)))
#or
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme) +
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint)) +
geom_point(size=5, shape=4) +
coord_flip()
Try this:
ggplot(df, aes(x = "", y = Endpoint, color = Var), colorscheme) +
geom_segment(aes(x = "", xend = "", y = Startpoint, yend = Endpoint), show.legend = FALSE) +
geom_point(aes(x = "", y = Endpoint), size = 5, shape = 4) +
coord_flip()
In this way legend will show only X
How can I change the colour for the two histograms plot made with facet_grid?
I would like to have the histogram of "Active" in green and "Failed" in red.
And also, how can I change the position of the text of the line in order to have it a bit more down?
Here is my code:
df %>%
ggplot(aes(x = `Banks/turnover%Last avail. yr`)) +
geom_histogram(
bins = nclass.Sturges(`Banks/turnover%Last avail. yr`),colour = "black")+
geom_vline(data = status_means, aes(xintercept = avg), color="red", linetype="dashed")+
geom_text(data = status_means , aes(x = avg, label = avg), y= Inf )+
theme(legend.position="None")+
facet_grid(~general_status)
You need to add a factor to the fill parameter. Here, I just use active-failed as I'm not sure how you have that distinguished in your data.
ggplot(df_all,
aes(x = `Banks/turnover%Last avail. yr`, fill = as.factor(active-failed)))
Then, you can add a line to define the colors:
scale_fill_manual(values = c("green", "red"))
As #teunbrand said, you can just use vjust to move the text so that it is not cut off.
Remember to give a good reproducible example, it is helpful to provide some of your data via dput(head(df)).
I'm using this data frame where Outcome is the color that I want to assign to the geom_segment.
hori5 +
geom_segment(data = shots.Barcelona.df,
aes(x = X.ShotLocation,xend = X.EndLocation, y = Y.ShotLocation,
yend = Y.EndLocation,colour = Outcome),
size=1,arrow=arrow(length = unit(0.01, "npc")))
Using this code, I wanted to get black or red arrows but for some reason I get strange colors. This is what I get instead:
Use scale_colour_identity(), e.g.
library(ggplot2)
dd <- data.frame(x=0:1,y=0:1,outcome=c("#000000","#FF0000"))
ggplot(dd,aes(x,y,colour=outcome)) + geom_point() + scale_colour_identity()
I'm trying to add shapes on the lines plotted using geom_freqpoly to give more visibility to them if the plot is printed b/w on paper.
data <- data.frame(time=runif(1000,0,20000),
class=c("a","b","c","d"))
ggplot(data, aes(time, colour = class)) + geom_freqpoly(binwidth = 1000) + geom_point(aes(shape=class))
but this generates this error:
'Error: geom_point requires the following missing aesthetics: y'
How can I solve this error?
Another thing is that I want to use a single colour (eg. blue) to draw the lines
but with scale_colour_brewer() I can't change the colour scale, I want to change it because the lightest colour is nearly white and you can barely see it.
How can I add a custom min and max for the colours?
How about this? The error you are getting is being produced by geom_point which needs x and y, so I removed it.
ggplot(data, aes(x = time, color = class)) +
geom_freqpoly(binwidth = 1000) +
scale_color_brewer(palette = "Blues") +
theme_dark()
If you don't want the dark background, pass manual values from RColorBrewer. The following example uses every second color to increase the contrast.
p1 <- ggplot(data, aes(x = time, color = class)) +
geom_freqpoly(binwidth = 1000) +
scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])
EDIT
You can extract summarised data from a ggplot object using layer_data function.
xy <- layer_data(p1)
ggplot(xy, aes(x = x, y = count, color = colour)) +
theme_bw() +
geom_line() +
geom_point() +
scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])
I have two histograms in one window (using facet) and I would like control over the color of the outline and the fill. I've tried looking up color scales, aes(), + color, + fill, including color and fill in qplot, all resulting in expected plots!
My code can be found below. (Note: mussel2 has two columns concentration (a list of numbers) and waterbody (listed or reference). I can provide the data if necessary.
I understand this is an elementary question, so I do appreciate your time.
qplot(data=mussel2,
x = Concentration,
xlim = x_lim,
ylim = y_lim,
xlab = expression(paste("Concentrations of DDE (", mu, "g/g)")),
ylab = "Frequency",
binwidth = 1.5)+
theme(legend.position="none")+
facet_grid(Waterbody~.)
If you want to keep the qplot format, try the following:
library(ggplot2)
qplot(diamonds$carat,
xlab="Carat",
geom="histogram",
ylab="Count",
binwidth=0.25,
fill=I("grey"),
col=I("black"))
Use ggplot if you want to tweak things. I left out some of your options, but you can probably work out how to put them in.
ggplot(data = mussel2, aes(x = Concentration)) +
geom_bar(binwidth = 1.5, fill = "firebrick4", color = "dodgerblue2") +
scale_x_continuous(limits = x_lim) +
labs(y = "Frequency") +
facet_wrap(~ Waterbody)