Behavior of "fill" argument in geom_polygon in R - r

I am trying to understand the behavior of the "fill" argument in geom_polygon for ggplot.
I have a dataframe where I have multiple values from a measure of interest, obtained in different counties for each state. I have merged my database with the coordinates from the "maps" package and then I call the plot via ggplot. I don't understand how ggplot chooses what color to show for a state considering that different numbers are provided in the fill variable (mean?median?interpolation?)
Reproducing a piece of my dataframe to explain what I mean:
state=rep("Alabama",3)
counties=c("Russell","Clay","Montgomery")
long=c(-87.46201,-87.48493,-87.52503)
lat=c(30.38968,30.37249,30.33239)
group=rep(1,3)
measure=c(22,28,17)
df=data.frame(state, counties, long,lat,group,measure)
Call for ggplot
p <- ggplot()
p <- p + geom_polygon(data=df, aes(x=long, y=lat, group=group, fill=df$measure),colour="black"
)
print(p)
Using the full dataframe, I have hundreds of rows with iterations of 17 counties and all the set of coordinates for the Alabama polygon. How is it that ggplot provides the state fill with a single color?
Again, I would assume it is somehow interpolating the fill values provided at each set of coordinate, but I am not sure about it.
Thanks everyone for the help.

Through trial and error, it looks like the first value of the fill mapping is used for the fill of the polygon. The range of the fill scale takes all values into account. This makes sense because the documentation doesn't mention any aggregation---I agree that an aggregate function would also make sense, but I would assume that the aggregation function would be set via an argument if that were the implementation.
Instead, the documentation shows an example (and recommends) starting with two data frames, one of which has coordinates for each vertex, and one which has a single row (and single fill value) per polygon, and joining them based on an ID column.
Here's a demonstration:
long=c(1, 1, 2)
lat=c(1, 2, 2)
group=rep(1,3)
df=data.frame(long,lat,group,
m1 = c(1, 1, 1),
m2 = c(1, 2, 3),
m3 = c(3, 1, 2),
m4 = c(1, 10, 11),
m5 = c(1, 5, 11),
m6 = c(11, 1, 10))
library(ggplot2)
plots = lapply(paste0("m", 1:6), function(f)
ggplot(df, aes(x = long, y = lat, group = group)) +
geom_polygon(aes_string(fill = f)) +
labs(title = sprintf("%s:, %s", f, toString(df[[f]])))
)
do.call(gridExtra::grid.arrange, plots)

Related

How do I plot a graph in R, with the first values in one colour and the next values in another colour?

I've been trying this out but I cannot find a solution. The best I can do is plotting the first 15846 values in 1 colour and then using the lines() function to add the remaining 841 points. But these then appear at the start of the graph and does not continue from the 15846th datapoint.
str(as.numeric(sigma.in.fr))
num [1:15846] 0.000408 0.000242 0.000536 0.000274 0.000476 ...
str(as.numeric(sigma.out.fr))
num [1:841] 0.002558 0.000428 0.000255 0.000549 0.00028 ...
plot(as.numeric(sigma.in.fr),type="l",col=c("tomato4"))
lines(as.numeric(sigma.out.fr), type="l",col="tomato1")
This returns the plot below:
Lets make some dummy data to demonstrate:
sigma.ins.fr = sin((1:800)/20) + rnorm(800)
sigma.outs.fr = sin((801:1000)/20) + rnorm(200)
Now, put all the data together into a single sequence
sigma.all = c(sigma.ins.fr, sigma.outs.fr)
And create an x vector which simply counts along the data. We'll need this in the segments call below.
x = seq_along(sigma.all)
Now create a vector of colors for the trace. It is the same length as the full data, with a color for each segment.
cols = c(rep("tomato4", length(sigma.ins.fr)), rep("blue", length(sigma.outs.fr)))
Now create a blank canvass on which to draw the data.
plot(sigma.all, type="l", col=NA)
At last, we can plot the data. Unfortunately, lines does not allow for a separate color in different segments. So instead we can use segments
segments(head(x,-1), head(sigma.all,-1), x[-1], sigma.all[-1], type="l", col=cols)
Or, if you really prefer to use two separate traces uning lines, then we can achieve this by adding the x coordinates to each call:
plot(sigma.all, type="l", col=NA)
lines(seq_along(sigma.ins.fr), sigma.ins.fr, col=c("tomato4"))
lines(seq_along(sigma.outs.fr) + length(sigma.ins.fr), sigma.outs.fr, col="tomato1")
Please provide a reproducible example. Using the packages ggplot2 and dplyr you can do something like:
df <- tibble(x = seq(1,1000, 1), y = seq(1, 500.5, 0.5))
ggplot() +
geom_line(data = df %>% filter(x < 800),
aes(x = x, y = y), color = "red", size =2) +
geom_line(data = df %>% filter(x >= 800),
aes(x = x, y = y),
color = "black", size = 2)
Note that I put the cut off at 800 (as I only created 1000 points), but you can easily change that.
So what I do is putting the data in geom_line, as you can also use this if you have different dataframes (with overlapping x and y) you want to plot in the same graph. However, I do filter the data at different points, so that different lines are drawn by the geom_line.

Changing colour under particular threshold for geom_line [duplicate]

I have the following dataframe that I would like to plot. I was wondering if it is possible to color portions of the lines connecting my outcome variable(stackOne$y) in a different color, depending on whether it is less than a certain value or not. For example, I would like portions of the lines falling below 2.2 to be red in color.
set.seed(123)
stackOne = data.frame(id = rep(c(1, 2, 3), each = 3),
y = rnorm(9, 2, 1),
x = rep(c(1, 2, 3), 3))
ggplot(stackOne, aes(x = x, y = y)) +
geom_point() +
geom_line(aes(group = id))
Thanks!
You have at least a couple of options here. The first is quite simple, general (in that it's not limited to straight-line segments) and precise, but uses base plot rather than ggplot. The second uses ggplot, but is slightly more complicated, and colour transition will not be 100% precise (but near enough, as long as you specify an appropriate resolution... read on).
base:
If you're willing to use base plotting functions rather than ggplot, you could clip the plotting region to above the threshold (2.2), then plot the segments in your preferred colour, and subsequently clip to the region below the threshold, and plot again in red. While the first clip is strictly unnecessary, it prevents overplotting different colours, which can look a bit dud.
threshold <- 2.2
set.seed(123)
stackOne=data.frame(id=rep(c(1,2,3),each=3),
y=rnorm(9,2,1),
x=rep(c(1,2,3),3))
# create a second df to hold segment data
d <- stackOne
d$y2 <- c(d$y[-1], NA)
d$x2 <- c(d$x[-1], NA)
d <- d[-findInterval(unique(d$id), d$id), ] # remove last row for each group
plot(stackOne[, 3:2], pch=20)
# clip to region above the threshold
clip(min(stackOne$x), max(stackOne$x), threshold, max(stackOne$y))
segments(d$x, d$y, d$x2, d$y2, lwd=2)
# clip to region below the threshold
clip(min(stackOne$x), max(stackOne$x), min(stackOne$y), threshold)
segments(d$x, d$y, d$x2, d$y2, lwd=2, col='red')
points(stackOne[, 3:2], pch=20) # plot points again so they lie over lines
ggplot:
If you want or need to use ggplot, you can consider the following...
One solution is to use geom_line(aes(group=id, color = y < 2.2)), however this will assign colours based on the y-value of the point at the beginning of each segment. I believe you want to have the colour change not just at the nodes, but wherever a line crosses your given threshold of 2.2. I'm not all that familiar with ggplot, but one way to achieve this is to make a higher-resolution version of your data by creating new points along the lines that connect your existing points, and then use the color = y < 2.2 argument to achieve the desired effect.
For example:
threshold <- 2.2 # set colour-transition threshold
yres <- 0.01 # y-resolution (accuracy of colour change location)
d <- stackOne # for code simplification
# new cols for point coordinates of line end
d$y2 <- c(d$y[-1], NA)
d$x2 <- c(d$x[-1], NA)
d <- d[-findInterval(unique(d$id), d$id), ] # remove last row for each group
# new high-resolution y coordinates between each pair within each group
y.new <- apply(d, 1, function(x) {
seq(x['y'], x['y2'], yres*sign(x['y2'] - x['y']))
})
d$len <- sapply(y.new, length) # length of each series of points
# new high-resolution x coordinates corresponding with new y-coords
x.new <- apply(d, 1, function(x) {
seq(x['x'], x['x2'], length.out=x['len'])
})
id <- rep(seq_along(y.new), d$len) # new group id vector
y.new <- unlist(y.new)
x.new <- unlist(x.new)
d.new <- data.frame(id=id, x=x.new, y=y.new)
p <- ggplot(d.new, aes(x=x,y=y)) +
geom_line(aes(group=d.new$id, color=d.new$y < threshold))+
geom_point(data=stackOne)+
scale_color_discrete(sprintf('Below %s', threshold))
p
There may well be a way to do this through ggplot functions, but in the meantime I hope this helps. I couldn't work out how to draw a ggplotGrob into a clipped viewport (rather it seems to just scale the plot). If you want colour to be conditional on some x-value threshold instead, this would obviously need some tweaking.
Encouraged by people in my answer to a newer but related question, I'll also share a easier to use approximation to the problem here.
Instead of interpolating the correct values exactly, one can use ggforce::geom_link2() to interpolate lines and use after_stat() to assign the correct colours after interpolation. If you want more precision you can increase the n of that function.
library(ggplot2)
library(ggforce)
#> Warning: package 'ggforce' was built under R version 4.0.3
set.seed(123)
stackOne = data.frame(id = rep(c(1, 2, 3), each = 3),
y = rnorm(9, 2, 1),
x = rep(c(1, 2, 3), 3))
ggplot(stackOne, aes(x = x, y = y)) +
geom_point() +
geom_link2(
aes(group = id,
colour = after_stat(y < 2.2))
) +
scale_colour_manual(
values = c("black", "red")
)
Created on 2021-03-26 by the reprex package (v1.0.0)

Doing path & point plot using qplot()

My data consists of three variables:volume, occ and state. I want to have a volume-occ path & point plot with the paths and points marked by different colours according to the state.
Here is my code:
qplot(occ,volume,data = data,geom=c('path','point'),color=factor(state))+scale_colour_manual(values=c("blue", "orange", "red"))
The outcome is like this:
It seems that the qplot() does not connect the points in the initial order.Because the red points and paths are not connected with others, so are the other two coloured points.
I guess qplot() reordered my data according to the variable 'state', and then plot the path within each state separately.
I also tried the code without the colour arguement:
qplot(occ,volume,data = data,geom=c('path','point'))
The outcome is like this:
This outcome does show the initial path order that I want.
What I want is every point being connected continuously in the initial order just like what outcome2 shows and marked by different colours according to the state variable.
What should I do with my code?
If you set the color to a variable the data are also grouped by this variable. To prevent this set the group attribute manually to a constant. Here is an example:
df <- data.frame(x = 1:20,
y = c(rnorm(10, 5, 2), rnorm(10, 5, 2)),
group = c(rep("a", 10), rep("b", 10) ))
ggplot(df, aes(x = x, y = y, group = 1, col = group))+ geom_path()
The same with qplot:
qplot(x,y,data = df,geom=c('point'),color=factor(group), group = 1)
So just add group = 1 to your code and it will work the way you expect it.

Draw vertical quantile lines over histogram

I currently generate the following plot using ggplot in R:
The data is stored in a single dataframe with three columns: PDF (y-axis in the plot above), mids(x) and dataset name. This is created from histograms.
What I want to do is to plot a color-coded vertical line for each dataset representing the 95th quantile, like I manually painted below as an example:
I tried to use + geom_line(stat="vline", xintercept="mean") but of course I'm looking for the quantiles, not for the mean, and AFAIK ggplot does not allow that. Colors are fine.
I also tried + stat_quantile(quantiles = 0.95) but I'm not sure what it does exactly. Documentation is very scarce. Colors, again, are fine.
Please note that density values are very low, down to 1e-8. I don't know if the quantile() function likes that.
I understand that calculating the quantile of an histogram is not quite the same as calculating that of a list of numbers. I don't know how it would help, but the HistogramToolspackage contains an ApproxQuantile() function for histogram quantiles.
Minimum working example is included below. As you can see I obtain a data frame from each histogram, then bind the dataframes together and plot that.
library(ggplot2)
v <- c(1:30, 2:50, 1:20, 1:5, 1:100, 1, 2, 1, 1:5, 0, 0, 0, 5, 1, 3, 7, 24, 77)
h <- hist(v, breaks=c(0:100))
df1 <- data.frame(h$mids,h$density,rep("dataset1", 100))
colnames(df1) <- c('Bin','Pdf','Dataset')
df2 <- data.frame(h$mids*2,h$density*2,rep("dataset2", 100))
colnames(df2) <- c('Bin','Pdf','Dataset')
df_tot <- rbind(df1, df2)
ggplot(data=df_tot[which(df_tot$Pdf>0),], aes(x=Bin, y=Pdf, group=Dataset, colour=Dataset)) +
geom_point(aes(color=Dataset), alpha = 0.7, size=1.5)
Precomputing these values and plotting them separately seems like the simplest option. Doing so with dplyr requires minimal effort:
library(dplyr)
q.95 <- df_tot %>%
group_by(Dataset) %>%
summarise(Bin_q.95 = quantile(Bin, 0.95))
ggplot(data=df_tot[which(df_tot$Pdf>0),],
aes(x=Bin, y=Pdf, group=Dataset, colour=Dataset)) +
geom_point(aes(color=Dataset), alpha = 0.7, size=1.5) +
geom_vline(data = q.95, aes(xintercept = Bin_q.95, colour = Dataset))

Heat Table in R

I'm looking for a way to duplicate the kind of Heat Table shown below with R (and possibly ggplot2). Specific time axis are irrelevant; any rectangular table should do.
I've tried to search for Heat map and Heat table in Google, but couldn't find any R package that did the trick.
Thoughts?
require(ggplot2)
df <- data.frame(vaxis = rep(c(letters[1:5], "top"), each = 4),
haxis = rep(c(letters[6:8], "right"), times = 6),
value = rpois(24, lambda = 10))
df$color <- factor(ifelse(df$vaxis == "top" | df$haxis == "right", 1, 0))
ggplot(df, aes(x = haxis, y = vaxis, size = value, color = color)) + geom_point()
Just get your data in a similar format. You could write a function to make the "top" and "right" values normalized marginal sums. Of course lots of tweaks are possible in naming, legends, theme, etc.

Resources