ggplot2 guide_colorstep add custom labels guide - r

I have a plot with a discrete colorscheme (109 different levels) and a colorstep legend.
The legend then shows all ticks for each color. How can I only show a few of the labels and ticks in the legend rather than all?
Plot 1 shows the legend when the labels are still there
Plot 2 shows the legend without the labels. I want to show only certain labels in the legend, to make it actually understandable (10 different values rather than 100). How do I do that?
My code is below, I am clearly doing something wrong, I just do not know what it is.
tt <- ggplot() +
# first layer: all countries, no fill, no white outlines
geom_polygon(data = maps2,
aes(x = long, y = lat, group = group), fill="grey", show.legend = F, size = 0.1) +
# second layer: only countries with a fill
geom_polygon(data = test,
aes(x = long, y = lat, group = group, fill = as.factor(tally)), show.legend = T)+
scale_fill_viridis_d(option="D", na.value = NA)+
facet_grid(~IO)+
theme( strip.text.x = element_blank())
tt + guides(fill=guide_colorsteps(label = F, title=element_blank())

Related

GGplot is adding geom_line item as a geom_bar item in legend

I'm making sediment profile grain size distribution graphs, with stacked bar charts representing sand, silt and clay and an added line showing the median value for each depth. The graph looks good, yet the legend of my final output is mixing up some of my items.
Here is a breakdown of my code:
GS_as = data.frame(Depth = c(10,30,50,70,90),
clay = c(0.99,0,0,2.86,3.62),
silt = c(55.48,81.48,53.26,79.5,70.71),
sand = c(43.53,18.52,46.74,17.64,25.67))
long = melt(GS_as,id = "Depth")
df = data.frame(Depth = c(10,30,50,70,90),
value = c(34.8,24.84,48.9,12.7,19.73),
variable = c("median","median","median","median","median"))
ggplot(long,aes(x=Depth,y=value,fill=variable)) +
geom_bar(stat="identity") + coord_flip() +
scale_y_continuous(position = "right") +
scale_x_continuous(breaks = seq(10,900,by = 20),trans='reverse') +
scale_fill_grey() +
geom_line(data=df, aes(x= Depth, y = value,group=variable,colour=variable)) +
geom_point(data=df,aes(x= Depth, y = value,group=variable,colour=variable))
The final output is giving me this graph 1
Now, how do I remove median from the legend grayscale of grain sizes, and how do i remove the points from each box in grayscale? The points should only be presented with the median as a separate variable. I've searched long to find a solution, but have not gotten anywhere. I'm guessing I got to my final graph by a strange unintuitive way.
Additionally, if its possible I would also like the median line and points to be black, remove the variables title and group all the items under 1 level.
I appreciate any help you can give.
To fix your first issue with the median showing up in the fill legend you could make fill a locale aes of geom_bar. For a black color you could set the color via scale_color_manual. The legend titles could be set or removed via labs and finally (and as far as I understand you) you could "group all the items under 1 level" via theme options by removing the spacing between the legends and almost all the margin around them.
library(ggplot2)
ggplot(long, aes(x = Depth, y = value)) +
geom_bar(aes(fill = variable), stat = "identity") +
coord_flip() +
scale_y_continuous(position = "right") +
scale_x_continuous(breaks = seq(10, 900, by = 20), trans = "reverse") +
scale_fill_grey() +
geom_line(data = df, aes(x = Depth, y = value, group = variable, colour = variable)) +
geom_point(data = df, aes(x = Depth, y = value, group = variable, colour = variable)) +
scale_color_manual(values = c("black")) +
labs(fill = NULL, color = NULL) +
theme(legend.spacing.y = unit(0, "pt"), legend.margin = margin(1, 0, 0, 0))

Remove a dark background from `geom_line` `ggplot2` legend

I am trying to build a plot with geom_rect and geom_line. I don't know how to remove dark color in the legend background after placing geom_line.
Here's my code
gp <- (gp
+ ggplot2::geom_rect(data = dd5,
mapping = ggplot2::aes(xmin = begin, xmax = end+expPer,
ymin = order+0.35, ymax = order+0.25,
fill = description),
show.legend = T)
+ ggplot2::geom_line(data = dd6e,
mapping = ggplot2::aes(x = begin,
y = order + 0.4*end,
group = order,
color = description),
show.legend = T)
+ guides(fill = guide_legend(override.aes = list(linetype = c(0))))
+ theme(axis.text.y=element_blank(), #remove y axis labels
axis.ticks.y=element_blank()) #remove y axis ticks
);
And here's the output, where the background is denoted in red.
Ideally, I'd also like to merge both descriptions into a single legend.
Cheers!
[EDIT] I tried:
scale_colour_manual and it only modifies the colour of the first line
scale_fill_manual and it modifies all rectangles except first one :/

ggplot2 - using two different color scales for same fill in overlayed plots

A very similar question to the one asked here. However, in that situation the fill parameter for the two plots are different. For my situation the fill parameter is the same for both plots, but I want different color schemes.
I would like to manually change the color in the boxplots and the scatter plots (for example making the boxes white and the points colored).
Example:
require(dplyr)
require(ggplot2)
n<-4*3*10
myvalues<- rexp((n))
days <- ntile(rexp(n),4)
doses <- ntile(rexp(n), 3)
test <- data.frame(values =myvalues,
day = factor(days, levels = unique(days)),
dose = factor(doses, levels = unique(doses)))
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot( aes(fill = dose))+
geom_point( aes(fill = dose), alpha = 0.4,
position = position_jitterdodge())
produces a plot like this:
Using 'scale_fill_manual()' overwrites the aesthetic on both the boxplot and the scatterplot.
I have found a hack by adding 'colour' to geom_point and then when I use scale_fill_manual() the scatter point colors are not changed:
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(fill = dose), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = factor(test$dose)),
position = position_jitterdodge(jitter.width = 0.1))+
scale_fill_manual(values = c('white', 'white', 'white'))
Are there more efficient ways of getting the same result?
You can use group to set the different boxplots. No need to set the fill and then overwrite it:
ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(group = interaction(day, dose)), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = dose),
position = position_jitterdodge(jitter.width = 0.1))
And you should never use data$column inside aes - just use the bare column. Using data$column will work in simple cases, but will break whenever there are stat layers or facets.

Producing a map with barplots using geom_subplot2d

I am trying to create a map with barplots for each state. I used geom_subplot2d. I am having a couple of issues and I was hoping somebody can help fix them
The border color I want it to be black but it is red no matter what value I use.
The color of the legend items (barplot) is also absorbed by each state. Is there a way that it can be separate?
Here is the code that produces the diagram:
p <- ggplot() +
geom_polygon(data = total,
aes(x = x, y = y, group = region, colour = "black",
fill = cut(eval(as.symbol(var1)), 10))) +
scale_fill_brewer(palette = "Blues")
testplot <- p +
geom_subplot2d(aes(long, lat, subplot = geom_bar(aes(yy, ..count.., fill = yy))),
bins = c(25,20), ref = NULL, width = rel(0.5), data = simdat) +
scale_fill_brewer(palette = "YlGn")
print(testplot)

ggplot2: legend of two different data sets

I am drawing a map with ggplot using a shape file. Then I add arcs using geom_line. The arcs are colored according to their type (oneway or twoway) and then I add nodes using geom_point. The nodes are colored according to their type (Origin, Destination, Node, Parking lot). I want to have two different legends: one for the node types and one for the arc types. Unfortunately, ggplot merges the legends and produces just one legend.
Here is the code (sorry that I can't provide a workable example. I can't send the shape files):
cityplot <- ggplot(data = s_zurich, aes(x = long, y = lat, group = id), fill = "white") +
geom_polygon(data = s_zurich, fill = "white") +
ylab("") + xlab("") +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank())
cityplot_arcs <- cityplot +
geom_line(data = allarcs, aes(x = X1, y = X2, group = Id, colour = Direction), size = 1) +
xlab("") + ylab("")
cityplot_arcs_nodes <- cityplot_arcs + geom_point(aes(x = lon, y = lat, colour = Type), shape = 15, size = 4, inherit.aes = FALSE, data = allnodes) +
theme(legend.position = "none")
Any help would be appreciated.
Here is a possible workaround. If you can keep your geom_polygon fill out of the aes() call - as looks to be the case above, then you can use a filled shape for the point (21 is a circle) and set the fill attribute rather than the color in the aes() call. See below:
mock_data<-
data.frame(x=sample(1:10,20,T),
y=sample(1:10,20,T),
direction=sample(c("1way","2way"),20,T),
type=sample(c("origin","destination","node","lot"),20,T))
ggplot(mock_data) +
geom_polygon(aes(x=c(0,12,12,0),y=c(0,0,12,12),id=c(1,1,1,1)),fill="white") +
geom_point(aes(x=x,y=y,fill=type),size=10,shape=21) +
geom_line(aes(x=x,y=y,color=direction),size=2) +
scale_fill_brewer(palette="Greens") + scale_color_brewer(palette="Set1")
Failing that, you can plot a mock legend only using ggplot() and use grid.arrange() to plot it next to your graph minus the default legend. Let me know in the comments if you need help with that.

Resources