I am using scale_fill_gradient2() and the colourbar that is created is showing decimal places. I tried to reproduce the text that shows decimals but could not but the text below is in scientific notations.
How can you round the numbers that displayed in the colourbar using scale_fill_gradient2()? For example I am seeing "25.00" and I'd like to show just "25"?
Also how can you set the labels manually? Let's say I want to look a the data and set labels like c(15, 25, 40)?
library(ggplot2)
dat <- data.frame(group = c(rep("A", 10), rep("B", 10)),
value = c(rnorm(10, 5,300), rnorm(10, 5000, 80000)))
ggplot(dat, aes(x = group, y = value, fill= value)) +
geom_bar(stat = "identity") +
scale_fill_gradient2(low = "red", mid = "yellow", high = "blue", midpoint = 0, name = "")
You can manually specify breaks and labels as needed.
ggplot(dat, aes(x = group, y = value, fill= value)) +
geom_bar(stat = "identity")+
scale_fill_gradient2(low = "red", mid = "yellow", high = "blue",
midpoint = 0, name = "",
breaks = c(0, 1e5, 2e5),
labels = c("0", "100,000", "200,000"))
Related
I'm producing a stacked bar plot with purple and yellow but the two colours won't show up. Here is my code:
ggplot(df_group, aes(x = COLOUR, y =COUNT, fill = GROUP,label=GROUP)) +
geom_bar(position="stack", stat = "identity") +
scale_fill_manual("Flower Colour", values = c("Purple" = "mediumpurple", "Yellow" = "Gold")) +
labs(x='Group', y="Visits")
What do I need to change?
I suspect that your code could not have created that plot. Your aesthetics are swapped around. Try this:
df <- data.frame(
COUNT = runif(n = 20, min = 0, max = 10),
GROUP = sample(c("bee", "beetle", "butterfly"), 20, replace = TRUE),
COLOUR = sample(c("Purple", "Yellow"), 20, replace = TRUE))
ggplot(df, aes(x = GROUP, y = COUNT, fill = COLOUR)) +
geom_bar(position = "stack", stat = "identity") +
scale_fill_manual("Flower Colour", values = c("Purple" = "mediumpurple", "Yellow" = "Gold")) +
labs(x='Group', y="Visits")
I currently have an issue regarding how the "fill" argument colors the different bars in my plot. I wish to have a predetermined ranking of the colors such that each value in the table that I'm plotting, corresponds to a color instead of in my case, where it varies. The way it works in my code is that the highest value in my table corresponds to the lightest color (in the default ggplot palette), and the lowest value corresponds to the darkest color. My wish specifically is that 100 should be what corresponds to the lightest color and 0 corresponding to the darkest color (as I'm plotting quantiles) and of course, everything in-between.
Here is my code.
library(tidyverse)
test <- tibble(factors=c("fact1","fact2","fact3","fact4"),
percent=c(18.3,42.6,81.1,62.8))
ggplot(test) +
geom_hline(
aes(yintercept = y),
data.frame(y = c(0:4) * 25),
color = "lightgrey"
) +
geom_col(
aes(
x = reorder(str_wrap((factors), 5), percent),
y = percent,
fill = percent
),
position = "dodge2",
show.legend = TRUE,
alpha = .9,
width=0.8
) +
geom_segment(
aes(
x = reorder(str_wrap(factors, 5), percent),
y = 0,
xend = reorder(str_wrap(factors, 5), percent),
yend = 100
),
linetype = "dashed",
color = "gray12"
) +
scale_y_continuous(
limits = c(-50, 100),
expand = c(0, 0),
breaks = c(0, 25, 50, 75,100)
)+
coord_polar()
If you change the values in the "percent" part of the "test" tibble, to something like percent=c(80.1,80.5,80.1,79.9) you'll quickly understand what I am talking about, as in my ideal case, It should only be showing four pretty much identical colors instead of the range of colors being determined from the highest to lowest of the "percent" column, showing dark to light colors.
Thanks in advance!
First you should create a palette:
my_palette <- colorRampPalette((RColorBrewer::brewer.pal(9, "Blues")))
Then you can add a scale_fill_gradientn to your ggplot:
test <- tibble(factors=c("fact1","fact2","fact3","fact4"),
# percent=c(18.3,42.6,81.1,62.8))
percent = c(80.1,80.5,80.1,79.9))
ggplot(test) +
geom_hline(
aes(yintercept = y),
data.frame(y = c(0:4) * 25),
color = "lightgrey"
) +
geom_col(
aes(
x = reorder(str_wrap((factors), 5), percent),
y = percent,
fill = percent
),
position = "dodge2",
show.legend = TRUE,
alpha = .9,
width=0.8
) +
geom_segment(
aes(
x = reorder(str_wrap(factors, 5), percent),
y = 0,
xend = reorder(str_wrap(factors, 5), percent),
yend = 100
),
linetype = "dashed",
color = "gray12"
) +
scale_y_continuous(
limits = c(-50, 100),
expand = c(0, 0),
breaks = c(0, 25, 50, 75,100)
)+
scale_fill_gradientn(colours = my_palette(100),
limits=c(0,100))+
coord_polar()
I'm trying to convert a ggplot2 image using ggplotly(), but it's not converting correctly. My ggplot image takes in two lines all of the time, and has a color variable for the geom_line component and a fill variable for the geom_point component.
Here is my code for the strictly the basic ggplot part:
test_data1 = data.frame(
filter = "Filter 1",
time = seq(as.Date("2017-01-01"), as.Date("2017-03-01"), "days"),
ovr_perc = rnorm(n = 60, mean = 8, sd = 2),
neg_perc = rnorm(n = 60, mean = 6, sd = 2),
count = sample(50:250,60,replace=T)
)
test_data2 = data.frame(
filter = "Filter 2",
time = seq(as.Date("2017-01-01"), as.Date("2017-03-01"), "days"),
ovr_perc = rnorm(n = 60, mean = 20, sd = 6),
neg_perc = rnorm(n = 60, mean = 6, sd = 2),
count = sample(50:250,60,replace=T)
)
test_data = rbind(test_data1, test_data2)
p = ggplot(test_data, aes(x=time, y=ovr_perc, group = factor(filter))) +
geom_line(aes(color=factor(filter))) +
geom_point(aes(fill=neg_perc, size = count), shape=21) +
scale_fill_gradient2(low = "darkgreen", mid = "yellow", high = "red1",
midpoint = 5, limits = c(0, 10), oob = squish) +
scale_colour_manual(values = c("pink", "green")) +
theme_classic()
p
The image should look something like this, which is almost exactly how I want it to look:
However, when I try adding the ggplotly code to get the tooltip feature, it turns into this:
ggplotly(p)
The entire color scheme on each of the points is gone. How can I change this so it keeps the same color scale from the ggplot image to the ggplotly image?
As noted in the open bug report linked above, geom_point's aesthetic mapping for fill doesn't work properly, while color works fine. According to the bug report, the problem goes away in the package's dev version, but if you don't have easy access to that (neither do I), the following works, at least on my end:
library(dplyr)
library(plotly)
p2 <- ggplot(test_data, aes(x=time, y=ovr_perc, linetype = filter)) +
geom_line(data = . %>% filter(filter == "Filter 1"),
colour = "pink") +
geom_line(data = . %>% filter(filter == "Filter 2"),
colour = "green") +
geom_point(aes(color = neg_perc, size = count)) +
scale_color_gradient2(low = "darkgreen", mid = "yellow", high = "red1",
midpoint = 5, limits = c(0, 10), oob = squish) +
scale_linetype_manual(values = c("solid", "solid"),
guide = guide_legend(override.aes = list(
color = c("pink", "green")
))) +
theme_classic(); p2
ggplotly(p2)
Explanations:
This uses color rather than fill for geom_point, which works fine.
The lines are plotted in separate geom_line layers, with their colours specified directly, outside aes().
The lines' linetype is mapped inside aes(), to force the creation of a legend for each filter value, while the actual mapping specified in scale_linetype_manual sets both lines to be solid.
In this SO answer, user #Crops shows how to add a legend to a ggalt::geom_dumbbell plot. Very nice.
library(ggalt)
df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80))
df2 = tidyr::gather(df, group, value, -trt)
ggplot(df, aes(y = trt)) +
geom_point(data = df2, aes(x = value, color = group), size = 3) +
geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1",
colour_x = "red", colour_xend = "blue",
dot_guide=TRUE, dot_guide_size=0.25) +
theme_bw() +
scale_color_manual(name = "", values = c("red", "blue") )
I want to sort trt descending on r. I tried replacing y = trt with y = reorder(trt, r), but I get an error that object r is not found.
Here is a way where we reorder the factor levels of trt in df and df2 before we plot.
# reorder factor levels
df$trt <- reorder(df$trt, df$r)
df2$trt <- factor(df2$trt, levels = levels(df$trt))
ggplot(df, aes(y = trt)) +
geom_point(data = df2, aes(x = value, color = group), size = 3) +
geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1",
colour_x = "red", colour_xend = "blue",
dot_guide=TRUE, dot_guide_size=0.25) +
theme_bw() +
scale_color_manual(name = "", values = c("red", "blue") )
Using the dumbbell package
##Reformat data
df3<-df %>% arrange(r)
df2<-df%>% mutate("key"="trt")
df2$trt<-factor(df2$trt,df3$trt)
##plot
dumbbell::dumbbell(df2, id="trt", column1="l", column2="r",key="key", delt =1, textsize=3, lab1 = "l", lab2="r", pt_val = 1, pointsize = 3,pt_alpha = 0.6, arrow=1, leg = "Add legend title", pval=2) + xlim(8,85) + facet_wrap(key ~.)
Added in some bells and whistles, you can remove them toggling with the options.
I dont have enough points to embed for here is the link. Hope someone finds it useful.
Has been now a few months I am working with ggplot2 but I still get stuck very easily on basic things, as the options out here are close to infinite.
Let's assume I have a simple plot created as follows:
set.seed(100)
df_1 = data.frame(lat = rnorm(20),
lon = rnorm(20),
x = rnorm(20))
library(ggplot2)
p = ggplot() +
geom_point(data = df_1,
aes(x=lon, y=lat, fill = x),
size = 5, colour = 'black', pch = 21) +
scale_fill_gradient2(low = "green", mid = 'white', high = "yellow",
breaks = c(-1, 0, 1),
labels = c('-1', '0', '1'),
limits = c(-1,1))
print(p)
How can I add a second legend with title (e.g. y) showing only one of those circles with white background and black contour?
To add extra element to legend, you have to add it to a plot. You can do this with:
geom_point(aes(alpha = ""), head(df_1, 1),
size = 5, fill = "white", pch = 21) +
Here we are adding first point in your dataset, setting it's fill and dummy alpha value (we need to set something within aes to add it to legend). I'm using "" so we won't have any text next to a point.
Also, it's important to add this point before main geom_point because it will cover original point (with white fill). You also need to reset alpha values from "" to 1 and to set wanted legend name for alpha in labs().
library(ggplot2)
ggplot(df_1, aes(lon, lat, fill = x)) +
geom_point(aes(alpha = ""), head(df_1, 1),
size = 5, fill = "white", pch = 21) +
geom_point(size = 5, pch = 21) +
scale_fill_gradient2(low = "green", mid = "white", high = "yellow",
breaks = c(-1, 0, 1),
labels = c("-1", "0", "1"),
limits = c(-1, 1)) +
scale_alpha_manual(values = 1) +
labs(alpha = "y")
PS. I have made some changes in your ggplot2 code:
You can specify data and aes within first ggplot call.
In geom layers aes is first argument, data is second. So instead of geom_point(data = df_1, aes(...). You have use geom_point(aes(...), df_1).
color = "black" is a default setting - you don't need to specify it.
You could add a factor with one level and use scale_color_manual:
set.seed(100)
df_1 = data.frame(lat = rnorm(20),
lon = rnorm(20),
x = rnorm(20),
new = rep('Coordinates', 20))
library(ggplot2)
p = ggplot() +
geom_point(data = df_1,
aes(x=lon, y=lat, fill = x, colour = new),
size = 5, pch = 21) +
scale_fill_gradient2(low = "green", mid = 'white', high = "yellow",
breaks = c(-1, 0, 1),
labels = c('-1', '0', '1'),
limits = c(-1,1)) +
scale_color_manual(name = "", values = "black")
print(p)