Adding geom_ribbon to geom_line from secondary dataframe - ggplot2

I'm trying to display my model fit and data points on the same plot with ggplot, but can't figure out how to add my calculated SE bands (pred$conf.low, pred$conf.high) to my model line (pred$predicted).
> head(toad2,5)
num DO
753 0 8.41
755 0 8.27
756 0 8.31
757 0 8.04
758 0 7.77
> str(toad2)
'data.frame': 640 obs. of 2 variables:
$ num: int 0 0 0 0 0 0 0 0 0 0 ...
$ DO : num 8.41 8.27 8.31 8.04 7.77 7.43 7.54 7.37 7.33 6.75 ...
- attr(*, "na.action")= 'omit' Named int [1:1005] 1 2 3 4 5 6 7 8 9 10 ...
..- attr(*, "names")= chr [1:1005] "1" "2" "3" "4" ...
fit <- glmmTMB(num ~ DO, family=nbinom2, ziformula = ~0, dispformula = ~1, toad2, REML = FALSE)
pred <- ggpredict(fit, terms = "DO")
> pred
# Predicted counts of num
DO | Predicted | 95% CI
-----------------------------
0 | 0.11 | [0.05, 0.24]
2 | 0.15 | [0.09, 0.25]
4 | 0.20 | [0.14, 0.27]
6 | 0.26 | [0.21, 0.33]
8 | 0.35 | [0.25, 0.49]
10 | 0.46 | [0.27, 0.80]
12 | 0.62 | [0.29, 1.33]
16 | 1.10 | [0.32, 3.79]
How would I add the 95% confidence intervals to this geom_line from ggeffects/data.frame "pred"?
ggplot(data = toad2, aes(x = DO, y = num)) +
geom_point(color='blue') +
geom_line(color='red',data = pred, aes(x=x, y=predicted))
My attempt:
ggplot(data = toad2, aes(x = DO, y = num)) +
geom_point(color='blue') +
geom_line(color='red',data = pred, aes(x=x, y=predicted, ymin = conf.low, ymax = conf.high))
Warning message:
In geom_line(color = "red", data = pred, aes(x = x, y = predicted, :
Ignoring unknown aesthetics: ymin and ymax
"pred" data:
> dput(pred)
structure(list(x = c(0, 2, 4, 6, 8, 10, 12, 14, 16), predicted = c(0.110979768096301,
0.147788011747898, 0.196804307587347, 0.262077654519131, 0.348999967735793,
0.464751478728963, 0.618893859452371, 0.824160065752538, 1.09750614520922
), std.error = c(0.385361502587751, 0.270114672803532, 0.167239363548879,
0.116295711211178, 0.1721307632046, 0.276191828818464, 0.391769667585664,
0.511110201467043, 0.632085771961832), conf.low = c(0.0521463010143289,
0.0870396588397944, 0.141801294508668, 0.208659856560919, 0.2490619680673,
0.270474215214499, 0.287171428211178, 0.302659126928022, 0.317961528540038
), conf.high = c(0.236191420812848, 0.250934995696594, 0.27314232651499,
0.329170632676038, 0.489038846134357, 0.798574965119925, 1.33380124775568,
2.24424031376646, 3.78825622175962), group = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "1", class = "factor")), row.names = c(NA,
-9L), class = c("ggeffects", "data.frame"), legend.labels = "1", x.is.factor = "0", continuous.group = FALSE, rawdata = structure(list(
response = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 2L, 1L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L,
2L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 2L, 0L, 0L, 0L, 0L, 1L, 7L, 0L, 2L, 0L, 1L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L,
1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 7.52, 7.35, 7.56,
7.02, 7.3, 7.03, 6.58, 6.49, 6.09, 5.44, 7.3, 5.04, 5.77,
5.26, 6.74, 7.49, 7.62, 7.1, 6.09, 5.31, 3.77, 4.81, 5.74,
5.61, 5.97, 5.22, 6.33, 9.09, 6.45, 6.11, 4.96, 4.8, 4.59,
5.07, 6.19, 5.77, 5.71, 5.75, 5.6, 5.95, 8.06, 7.8, 5.88,
5.13, 5.97, 7.17, 5.91, 5.97, 5.82, 5.02, 5.93, 5.79, 4.72,
4.89, 5.69, 3.95, 3.47, 3.48, 9.64, 7.01, 6.13, 5.81, 4.28,
4.42, 3.18, 3.91, 2.4, 7.42, 9.88, 9.22, 7.77, 7.32, 7.03,
9.98, 7.15, 7.49, 8.26, 6.94, 7.05, 5.05, 6.65, 7.63, 9.48,
9.3, 9.26, 8.97, 12.67, 10.21, 14.43, 10.71, 6.01, 7.25,
5.83, 5.48, 4.85, 5.23, 3.74, 8.19, 6.38, 5.17, 8.11, 8.15,
5.72, 5.63, 5.48, 4.29, 9.74, 9.88, 7.32, 8.69, 7.26, 5.05,
9.22, 9.51, 8.94, 8.68, 8.82, 8.14, 5.57, 8.09, 7.92, 11.72,
7.2, 7.84, 12.55, 7.18, 6.47, 7.12, 7.29, 4.94, 12.07, 8.95,
11.45, 10.5, 6.88, 5.8, 7.16, 6.4, 5.25, 4.68, 5.14, 7.05,
4.46, 4.4, 11.91, 9.32, 7.8, 7.85, 8.04, 7.98, 6.96, 6.7,
3.56, 8.2, 5.74, 6.78, 7.08, 6.99, 4.9, 6.55, 3.51, 6.23,
4.93, 4.83, 4.17, 3.96, 4.45, 3.43, 2.74, 2.62, 1.56, 4.01,
4.1, 4.93, 4.17, 9.61, 6.55, 4.43, 4.79, 3.92, 3.43, 3.31,
2.56, 0.82, 1.54, 5.17, 7.17, 3.28, 4.14, 1.79, 2.87, 3.52,
2.06, 0.66, 10.28, 8.22, 7.41, 5.17, 4.29, 1.29, 10.2, 6.8,
5, 6.6, 5.5, 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7, 9.5, 6.8, 6.6, 6.1, 6.7, 6.6, 6.3, 6.6,
7.9, 6.5, 5.2, 5.9, 6.7, 6.3, 4.4, 4.7, 6.5, 3.67, 5.2, 6.24,
5.76, 6.73, 6.16, 6.9, 7.16, 3.92, 2.03, 2.17, 3.63, 1.81,
1.85, 4.25, 0.69, 3.52, 2.25, 7.83, 2.45, 3.41, 2.49, 6.24,
6.77, 5.26, 4.92, 5.03, 4.3, 4.33, 3.67, 10.73, 7.43, 6.11,
6.3, 5.11, 3.56, 11.02, 8.99, 6.68, 10.7, 6.88, 5.92, 5.5,
5.76, 5.2, 3.44, 3.04, 1.05, 2.96, 2.64, 2.16, 2.55, 3.34,
3.14, 3.76, 4.6, 5.03, 4.04, 2.77, 4.11, 3.06, 4.32, 4.19,
4.32, 4.36, 4.2, 3.03, 5.31, 5.4, 4.83, 4.68, 4.7, 4.27,
4.4, 4.61, 5.28, 4.11, 3.95, 3.51, 3.59, 3.12, 2.5, 2.32,
2.55, 3.05, 4.43, 4.05, 3.42, 1.26, 3.91, 3.93, 3.41, 4.61,
6.42, 6.73, 5.04, 4.24, 5.61, 4.74, 6.19, 6.45, 6.94, 6.17,
6.51, 5.42, 5.66, 7.64, 5.45, 5.21, 4.45, 3.02, 2.05, 4.59,
8.23, 5.56, 4.77, 7.87, 7.36, 5.72, 4.09, 4.98, 5.02, 6.03,
5.68, 5.73, 5.94, 6.07, 5.97, 6.01, 5.54, 5.34, 4.29, 4.45,
4.77, 5.16, 4.55, 4.6, 5.73, 5.16, 5.41, 5.88, 1.63, 2.71,
4.64, 1.02, 3.04, 3.02, 2.81, 2.69, 3.35, 4.83, 3.56, 3.19,
4.65, 3.93, 3.76, 3.73, 6.62, 6.7, 5.49, 0.93, 1.12, 0.9,
1.96, 1.5, 1.32, 2.98, 3.2, 3.23, 3.52, 3.08, 3.73, 3.95,
3.21, 4.14, 4.34, 3.48, 3.25, 0.61), group = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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= "data.frame", row.names = c(NA,
-640L)), title = "Predicted counts of num", x.title = "DO", y.title = "num", legend.title = NA_character_, constant.values = structure(list(), names = character(0)), terms = "DO", original.terms = "DO", at.list = list(
DO = c(0, 2, 4, 6, 8, 10, 12, 14, 16)), prediction.interval = FALSE, ci.lvl = 0.95, type = "fe", response.name = "num", family = "nbinom2", link = "log", logistic = "0", link_inverse = function (eta)
pmax(exp(eta), .Machine$double.eps), link_function = function (mu)
log(mu), is.trial = "0", fitfun = "glm", model.name = "fit")

Using a geom_ribbon you could do:
library(ggplot2)
ggplot() +
geom_ribbon(data = pred, aes(x=x, ymin = conf.low, ymax = conf.high), alpha = .3, fill = "red") +
geom_line(color = "red", data = pred, aes(x = x, y = predicted))

Related

How to completely remove left and bottom white margins of matplotlib draw?

import numpy as np
from matplotlib import pyplot as plt
data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.figure(figsize=(6, 4))
im = plt.imshow(data, cmap="YlGn")
linewidth = 2
for axis in ['top', 'bottom', 'left', 'right']:
plt.gca().spines[axis].set_linewidth(linewidth)
plt.gca().set_xticks(np.arange(data.shape[1] + 1) - .5, minor=True)
plt.gca().set_yticks(np.arange(data.shape[0] + 1) - .5, minor=True)
plt.gca().grid(which="minor", color="black", linewidth=linewidth)
plt.gca().tick_params(which="minor", bottom=False, left=False)
plt.tight_layout()
plt.gca().set_xticks(ticks=[])
plt.gca().set_yticks(ticks=[])
plt.savefig("test.pdf",
bbox_inches="tight",
transparent="True",
pad_inches=1.0/72.0 * linewidth / 2.0)
This code will output the following pdf, but you can see that there are white borders on the left and bottom, so the pdf is not centered after being inserted into LaTex. How to solve this problem?
plt result:
import numpy as np
from matplotlib import pyplot as plt
data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.figure(figsize=(6, 4))
im = plt.imshow(data, cmap="YlGn")
linewidth = 2
for axis in ['top', 'bottom', 'left', 'right']:
plt.gca().spines[axis].set_linewidth(linewidth)
plt.gca().set_xticks(np.arange(data.shape[1] + 1) - .5, minor=True)
plt.gca().set_yticks(np.arange(data.shape[0] + 1) - .5, minor=True)
plt.gca().grid(which="minor", color="black", linewidth=linewidth)
plt.gca().tick_params(which="minor", bottom=False, left=False)
plt.tight_layout()
plt.gca().set_xticks(ticks=[])
plt.gca().set_yticks(ticks=[])
plt.gca().tick_params(axis="both",
which="major",
left=False,
bottom=False,
labelleft=False,
labelbottom=False)
plt.savefig("test.pdf",
bbox_inches="tight",
transparent="True",
pad_inches=1.0 / 72.0 * linewidth / 2.0)
It was an issue with ticks, solved now.

How to automatically color matplotlib graphics using same color as pre-existing label?

I am plotting multiple instances of data that use the same label. There are techniques to remove duplicates from the legend, shown here.
However, I additionally want to prevent different colors being used for the same label.
If I call
import matplotlib.pyplot as plt
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a')
plt.plot([1,2,3], [1.5, 4.0, 3.0], label='a')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b')
plt.plot([1,2,3], [1.5, 2.5, 1.0], label='b')
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
unique = [(h, l) for i, (h, l) in enumerate(zip(handles, labels)) if l not in labels[:i]]
ax.legend(*zip(*unique))
I am hoping for a plot like:
However, I get this plot:
Is there a way I can get the former plot without having to explicitly set the colors, like so:
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a', color='C0')
plt.plot([1,2,3], [1.5, 4.0, 3.0], label='a', color='C0')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b', color='C1')
plt.plot([1,2,3], [1.5, 2.5, 1.0], label='b', color='C1')
I think the most straight-forward would be to just remove the labels for all but one of the lines for each label:
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a', color='C0')
plt.plot([1,2,3], [1.5, 4.0, 3.0], color='C0')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b', color='C1')
plt.plot([1,2,3], [1.5, 2.5, 1.0], color='C1')
plt.legend()
You could achieve the same by using an initial underscore for any labels you don't want to show:
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a', color='C0')
plt.plot([1,2,3], [1.5, 4.0, 3.0], label='_a', color='C0')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b', color='C1')
plt.plot([1,2,3], [1.5, 2.5, 1.0], label='_b', color='C1')
plt.legend()
I find this sometimes useful for plotting in a loop (inserting '_' to the start of a label depending on the iteration in the loop, perhaps including a list of colors to set the color for each line - though this would be trivial in your example).
I can't think of a scenario where you actually need matplotlib to "know" that each line is part of the same label. These type of approaches have been fine in my experience.

An easier way to manipulate my data?

I have a species x site matrix with treatment types and months attached to each site sampled. I've managed to restructure my data in the way I want using a bunch of loops and extra lines of code but I'm curious to how I can code this in a more efficient manner that works as new data comes in.
Data:
time<-structure(list(Month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("February", "March"), class = "factor"), Treatment = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 1L, 1L), .Label = c("C", "T"), class = "factor"),
Site = structure(c(13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L), .Label = c("SHA10RA", "SHA10RB",
"SHA10RC", "SHA10RD", "SHA10RE", "SHA10RF", "SHA11RA", "SHA11RB",
"SHA11RC", "SHA11RD", "SHA11RE", "SHA11RF", "SHA1RA", "SHA1RB",
"SHA1RC", "SHA4RA", "SHA5RA", "SHA5RB", "SHA5RC", "SHA5RD"
), class = "factor"), Pheidole.adrianoi = c(0L, 5L, 0L, 1L,
0L, 0L, 0L, 0L, 3L, 4L, 1L, 4L, 3L, 2L, 0L, 0L, 0L, 1L, 0L,
0L, 1L, 5L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L,
0L, 0L, 0L, 4L, 0L, 0L), Pheidole.floridana = c(1L, 1L, 6L,
1L, 7L, 6L, 0L, 0L, 0L, 0L, 1L, 0L, 3L, 1L, 4L, 3L, 3L, 5L,
5L, 0L, 0L, 0L, 2L, 1L, 6L, 5L, 1L, 3L, 0L, 0L, 3L, 0L, 2L,
1L, 4L, 5L, 5L, 0L, 7L, 0L), Pheidole.metallescens = c(5L,
0L, 4L, 2L, 0L, 0L, 0L, 0L, 2L, 2L, 2L, 1L, 0L, 4L, 0L, 0L,
0L, 1L, 0L, 0L, 3L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 2L, 1L,
0L, 2L, 3L, 0L, 0L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L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Month",
"Treatment", "Site", "Pheidole.adrianoi", "Pheidole.floridana",
"Pheidole.metallescens", "Solenopsis.carolinensis", "Solenopsis.invicta",
"Solenopsis.nickersoni", "Solenopsis.pergandei", "Solenopsis.tennesseensis",
"Solenopsis.tonsa"), class = "data.frame", row.names = c(NA,
-40L))
code:
Species<-colnames(time)[4:12]
Sites<-time$Site
Treatment<-time$Treatment
Treatment<-as.character(Treatment)
Month<-time$Month
Month<-as.character(Month)
Species<-rep(Species,40)
Sites<-as.character(Sites)
Site<-NULL
for (i in 1:length(Sites)) {
Site1<-rep(Sites[i],9)
Site<-append(Site,Site1)
}
Site
as.vector(Site)
length(Species)
length(Site)
Period<-NULL
for (i in 1:length(Month)) {
Month1<-rep(Month[i],9)
Period<-append(Period,Month1)
}
Period
Type<-NULL
for (i in 1:length(Treatment)) {
Treat1<-rep(Treatment[i],9)
Type<-append(Type,Treat1)
}
Type
Abundance<-NULL
for (i in 1:40) {
Occurence<-t(time[i,4:12])
Abundance<-append(Abundance,Occurence)
}
Abundance
length(Abundance)
class(Abundance)
df1<-data.frame(Period,Type,Site, Species, Abundance)
df1
I know the package reshape2 could probably help me but I am having trouble understanding exactly how I can optimize it for my use. Any help would be great!

How to create a new tensor in this situation (derive b from a)?

I have a tensor 'a', I want to modify a element of it.
a = tf.convert_to_tensor([[1.0, 1.0, 1.0],
[1.0, 2.0, 1.0],
[1.0, 1.0, 1.0]], dtype=tf.float32)
And I can got the index of that element.
index = tf.where(a==2)
How to derive 'b' from 'a'?
b = tf.convert_to_tensor([[1.0, 1.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0]], dtype=tf.float32)
I know that I can't not modify a tensor from this post.
I solve it by using tf.sparse_to_dense()
import tensorflow as tf
a = tf.convert_to_tensor([[1.0, 1.0, 1.0],
[1.0, 2.0, 1.0],
[1.0, 1.0, 1.0]], dtype=tf.float32)
index = tf.where(a > 1)
zero = tf.sparse_to_dense(index, tf.shape(a, out_type=tf.int64), 0., 1.)
update = tf.sparse_to_dense(index, tf.shape(a, out_type=tf.int64), 0., 0.)
b = a * zero + update
with tf.Session() as sess:
print sess.run(b)

Matplotlib:empty confusion matrix

Need to plot a confusion matrix with this script. By running it an empty plot appears. Seems I am close to solution. Any hint?
from numpy import *
import matplotlib.pyplot as plt
from pylab import *
conf_arr = [[50.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [3.0, 26.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0], [4.0, 1.0, 0.0, 5.0, 0.0, 0.0, 0.0], [3.0, 0.0, 1.0, 0.0, 6.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 47.0, 0.0], [2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8.0]]
norm_conf = []
for i in conf_arr:
a = 0
tmp_arr = []
a = sum(i,0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111)
res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
cb = fig.colorbar(res)
savefig("confmat.png", format="png")
Thanks, I have the plot. Now, the ticks in the x-axes are very small (the graph dimension is: 3 cm x 10 cm or so). How can I enlarge them in order to have a more proportioned graph, lets say 10cm x 10 cm plot? A possible reason is that I visualize the graph as a subplot? Was not able to find the suitable literature to adjust that.
You don't need to clear a current figure (plt.clf()) before adding a new one.
#plt.clf() # <<<<< here
fig = plt.figure()
ax = fig.add_subplot(111)