How do you fix warning message: colourbar guide needs continuous scales? - r

I would like to produce multiple contour plots using ggplot2 and
geom_contour_filled()
but the z values range is too large. To give you a little bit of an idea of what the values are, it ranges from -2,71 to -157,28. So I thought I should change the breaks so it covers all of these values.
The code below is not the data I work with, but it should represent the problem I have:
The data
h_axis <- 10^(seq(log10(0.1), log10(1000),
length.out = 20))
a_axis <- 10^(seq(log10(0.1), log10(1000),
length.out = 20))
comb <- expand.grid(h_axis, a_axis)
h_val <- comb$Var2
a_val <- comb$Var1
values <- seq(-2, -150, length.out = 400)
dt <- data.frame(h = h_val, a = a_val, values)
First, let's say I don't change the breaks. Then, using this code
ggplot(dt, aes(x = log10(h_val), y = log10(a_val), z = values)) +
geom_contour_filled() +
# geom_contour(color = "black", size = 0.1) +
xlab(expression(log[10](h))) +
ylab(expression(log[10](a))) +
guides(fill = guide_colorbar(title = expression('E ||'*g - hat(g)*'||'[2]*'')))
will produce the following figure:
So a lot of the area will be covered by the same colour, which is a problem since my data consists of multiple factors. Factor 1 is covered by the yellow, Factor 2 is covered by the green, and so on.
Then my second approach, is to add
bar <- 10^(seq(log10(-min(values)), log10(-max(values)),
length.out = 100))
and put bar in the geom_contour_filled() like this
geom_contour_filled(breaks = -bar)
Then I get
which is nice! But, in both cases I get the following warning
Warning message:
colourbar guide needs continuous scales.
Also, the legend is not shown on the right side. What do I need to do to fix the warning and how can I make sure that the legend is shown?

Try guide_legend instead of guide_colorbar.

Related

Geom_dotplot, y-axis showing counts

I'm trying to plot a dotplot using geom_dotplot in which each dot represents an observation of my data set. Therefore, the y-axis shouldn't represent density but actual counts. I'm aware of this thread which revolves around the same topic. However, I haven't managed to solve my issue following the same methodology.
df <- data.frame(x = sample(1:500, size = 150, replace = TRUE))
ggplot(df, aes(x)) +
geom_dotplot(method = 'histodot', binwidth = 1)
And I obtain the following graph , I want to obtain one similar to this one where I can manipulate dots' size, space between, etc.
Thanks in advance
You can modify the binwidth argument to cause the points to stack. For example,
df %>%
ggplot(aes(x = x)) +
geom_dotplot(method = "histodot", binwidth = 20)
There is a dotsize argument that can be used to modify the size of the dot.

Color horizontal bars in ggplot's geom_bar

I'm having some trouble with ggplot2's geom_bar:
Here's my data:
set.seed(1)
df <- data.frame(log10.p.value = -10*log10(runif(10,0,1)), y = letters[1:10], col = rep("#E0E0FF",10), stringsAsFactors = F)
#specify color by log10.p.value
df$col[which(df$log10.p.value > 2)] <- "#EBCCD6"
df$col[which(df$log10.p.value > 4)] <- "#E09898"
df$col[which(df$log10.p.value > 6)] <- "#C74747"
df$col[which(df$log10.p.value > 8)] <- "#B20000"
#truncate bars
df$log10.p.value[which(df$log10.p.value > 10)] <- 10
As you can see each log10.p.value interval is assigned a different color and since I don't want the bars to extend beyond log10.p.value = 10 I set any such value to 10.
My ggplot command is:
p <- ggplot(df, aes(y=log10.p.value,x=y,fill=as.factor(col)))+
geom_bar(stat="identity",width=0.2)+coord_flip()+scale_y_continuous(limits=c(0,10),labels=c(seq(0,7.5,2.5)," >10"))+
theme(axis.text=element_text(size=10))+scale_fill_manual(values=df$col,guide=FALSE)
And the figure is:
The problems are:
The bar colors in the plot do not match df$col. For example, bars a and b are colored #EBCCD6 rather than #E09898.
Because I manually specify the x-axis last tick text to be ">10" an extra space is created in the plot to the right of that tick making the bars I truncated at 10 seem like they end at 10, whereas my intention was for them to go all the way to the right end of the plot.
I am unable to reproduce the graph that you generated. Running the code you've provided generates the following graph:
You can correctly specify the colours by naming the vector that you are passing to scale_fill_manual:
coloursv <- df$col
names(coloursv) <- df$col
To the second part of your question - you can make sure there is no space between the bars and the edge of the graph using the expand parameter of scale_y_continuous.
Making these two changes, the code for the plot becomes:
p <- ggplot(df, aes(y=log10.p.value,x=y,fill=as.factor(col)))+
geom_bar(stat="identity",width=0.2) +
scale_y_continuous(limits=c(0,10),
labels=c(seq(0,7.5,2.5)," >10"),
expand = c(0,0)) +
theme(axis.text=element_text(size=10)) +
scale_fill_manual(values = coloursv,guide = F) +
coord_flip()
The '>10' label is a bit cut-off. You can increase the plot margins to make it visible, using:
p + theme(plot.margin=unit(c(0.1,0.5,0.1,0.1),"cm"))

Label minimum and maximum of scale fill gradient legend with text: ggplot2

I have a plot created in ggplot2 that uses scale_fill_gradientn. I'd like to add text at the minimum and maximum of the scale legend. For example, at the legend minimum display "Minimum" and at the legend maximum display "Maximum". There are posts using discrete fills and adding labels with numbers instead of text (e.g. here), but I am unsure how to use the labels feature with scale_fill_gradientn to only insert text at the min and max. At the present I am apt to getting errors:
Error in scale_labels.continuous(scale, breaks) :
Breaks and labels are different lengths
Is this text label possible within ggplot2 for this type of scale / fill?
# The example code here produces an plot for illustrative purposes only.
# create data frame, from ggplot2 documentation
df <- expand.grid(x = 0:5, y = 0:5)
df$z <- runif(nrow(df))
#plot
ggplot(df, aes(x, y, fill = z)) + geom_raster() +
scale_fill_gradientn(colours=topo.colors(7),na.value = "transparent")
For scale_fill_gradientn() you should provide both arguments: breaks= and labels= with the same length. With argument limits= you extend colorbar to minimum and maximum value you need.
ggplot(df, aes(x, y, fill = z)) + geom_raster() +
scale_fill_gradientn(colours=topo.colors(7),na.value = "transparent",
breaks=c(0,0.5,1),labels=c("Minimum",0.5,"Maximum"),
limits=c(0,1))
User Didzis Elfert's answer slightly lacks "automatism" in my opinion (but it is of course pointing to the core of the problem +1 :).
Here an option to programatically define minimum and maximum of your data.
Advantages:
You will not need to hard code values any more (which is error prone)
You will not need hard code the limits (which also is error prone)
Passing a named vector: You don't need the labels argument (manually map labels to values is also error-prone).
As a side effect you will avoid the "non-matching labels/breaks" problem
library(ggplot2)
foo <- expand.grid(x = 0:5, y = 0:5)
foo$z <- runif(nrow(foo))
myfuns <- list(Minimum = min, Mean = mean, Maximum = max)
ls_val <- unlist(lapply(myfuns, function(f) f(foo$z)))
# you only need to set the breaks argument!
ggplot(foo, aes(x, y, fill = z)) +
geom_raster() +
scale_fill_gradientn(
colours = topo.colors(7),
breaks = ls_val
)
# You can obviously also replace the middle value with sth else
ls_val[2] <- 0.5
names(ls_val)[2] <- 0.5
ggplot(foo, aes(x, y, fill = z)) +
geom_raster() +
scale_fill_gradientn(
colours = topo.colors(7),
breaks = ls_val
)

How to suppress zeroes when using geom_histogram with scale_y_log10

I'm trying to plot a histogram with a log y scale using ggplot, geom_histogram and scale_y_log10. Most regions (those with counts greater than 1) appear correct: the background is transparent and the histogram bars are filled with the default color black. But at counts of 1, the colors are inverted: black background and transparent fill of the histogram bars. This code (below) generates the example in the graph.
Can anyone explain the cause of this? I understand the problems that come with log scales but I can't seem to find a solution to this. I'm hoping there's a easy fix, or that I overlooked something.
set.seed(1)
df <- data.frame(E=sample(runif(100), 20, TRUE))
ggplot(df,aes(E)) + geom_histogram(binwidth=0.1) + scale_y_log10(limits=c(0.1,100)) + xlim(0,1)
You can add drop=TRUE to the geom_histogram call to drop bins with zero counts (see ?stat_bin for details):
set.seed(1)
df <- data.frame(E=sample(runif(100), 20, TRUE))
ggplot(df,aes(E)) +
geom_histogram(binwidth=0.1, drop=TRUE) +
scale_y_log10(limits=c(0.1,100)) +
xlim(0,1)
EDIT: Since the scale starts at 1, it is impossible to display a bar of height 1. As mentioned in this answer, you can choose to start at different levels, but it may become misleading. Here's the code for this anyway:
require(scales)
mylog_trans <-
function (base = exp(1), from = 0)
{
trans <- function(x) log(x, base) - from
inv <- function(x) base^(x + from)
trans_new("mylog", trans, inv, log_breaks(base = base), domain = c(base^from, Inf))
}
ggplot(df,aes(E)) +
geom_histogram(binwidth=0.1, drop=TRUE) +
scale_y_continuous(trans = mylog_trans(base=10, from=-1), limits=c(0.1,100)) +
xlim(0,1)

R: why is my ggplot geom_point() symbol not visible?

I am trying to place a symbol on the lowest point in a certain time series, which I have plotted with ggplot's geom_line. However, the geom_point is not showing up on the plot. I have myself successfully used geom_point for this kind of thing before by following hadley's example here (search for 'highest <- subset' to get the relevant assignment) so I know very well that it can be done. I'm just at a loss to spot what I have done differently here that is causing it not to display. I'm guessing it's something straightforward like a missing argument or similar - easy points for a pair of fresh eyes, I think.
Minimal example follows:
require(ggplot2)
fstartdate <- as.Date('2009-06-01')
set.seed(12345)
x <- data.frame(mydate=seq(as.Date("2003-06-01"), by="month", length.out=103),myval=runif(103, min=180, max=800))
lowest <- subset(x, myval == min(x[x$mydate >= fstartdate,]$myval))
thisplot <- ggplot() +
geom_line(data = x, aes(mydate, myval), colour = "blue", size = 0.7) +
geom_point(data = lowest, size = 5, colour = "red")
print(thisplot)
The point appears if you add the aesthetic:
thisplot + geom_point(
data = lowest,
aes(mydate, myval),
size = 5, colour = "red"
)

Resources