Using the following code I draw a series of lines and 95% confidence intervals using geom_line
and geom_ribbon
.
I would like to smooth the plotting of the mean line and the confidence interval shadings. I have tried to use geom_smooth
, but I don't get the desired setting.
library(dplyr)
library(ggplot2)
library(scales)
tribble( ~y, ~z, ~g,
1 ,"A", 1,
2 ,"A", 1,
2 ,"B", 1,
3 ,"B", 1,
1.5 ,"C", 1,
2 ,"C", 1,
2 ,"D", 1,
2 ,"D", 1,
3 ,"A", 2,
3.5 ,"A", 2,
0.5 ,"B", 2,
2 ,"B", 2,
2 ,"C", 2,
2 ,"C", 2,
1.5 ,"D", 2,
1 ,"D", 2,
4 ,"A", 3,
3.5 ,"A", 3,
5.5 ,"B", 3,
3 ,"B", 3,
3.3 ,"C", 3,
2.1 ,"C", 3,
1.3 ,"D", 3,
1.2 ,"D", 3)-> data
z = factor(z, levels = c("A", "B", "C", "D"))
data %>%
group_by(g, z) %>%
summarise(media = mean(y),
desvio = sd(y),
error_est = desvio / sqrt(n()),
intervalo_sup = media + (2*error_est),
intervalo_inf = media - (2*error_est)) %>%
ggplot(aes(x = g, y = media, color = z)) +
geom_line(aes(group = z, color = z), size=0.5) +
geom_ribbon(aes(x = as.numeric(g), ymax = intervalo_sup,
ymin = intervalo_inf,
fill = z),
alpha=0.3, colour = NA) +
labs(x = "X", y = "Y", fill = "Condition", color = "Condition") +
scale_fill_manual(labels = c("A", "B", "C", "D"), values = c("blue","green","red", "black")) +
scale_color_manual(labels = c("A", "B", "C", "D"), values = c("blue","green","red", "black")) +
scale_x_discrete(breaks = seq(1, 3, by = 1)) +
theme(legend.position="bottom", legend.text=element_text(size=12)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14))
Unfortunately the graphs of R in Windows , in the graphs panel, do not look quite right. In the case of the lines, it is extremely remarkable that they are seen with a "jagged" style. Making a "zoom" to your graph makes this effect very noticeable:
It is normal for this to happen, any line that is not perfectly horizontal or vertical, on any monitor will suffer from this effect. What happens is that in some cases this situation is contemplated and aliasing techniques are implemented ("aliasing"), which basically add "pixels" of softer colors, which makes us perceive the lines much better. Windows implements font anti-aliasing, but not the other type of graph, unfortunately to date, there is no solution , although there is an improvement scheduled for this in Rstudio .
Both Linux and MAC have better implementations for displaying graphics, so this problem is greatly minimized on these operating systems. Particularly on Linux (on Mac I think too), Cairo is used by default , which solves this problem much better.
A palliative solution is to take advantage of the fact that "Cairo" can be configured as the default device to save a graph
ggplot
, so saving the graph in apng
, for example, this will not have the problems that we noticed in the graphs panel of Rstudio . We are talking about the routineggsave()
that you can implement at the end of your batch, with which you will see the graph as usual in the panel, but additionally you will save a correctly smoothed image:Final example (we attach example.png):
And if we do a "zoom"
We see a noticeable improvement in the lines, and a minor improvement in the shadows or areas.
Addendum as of 6/8/2021
Apparently from this version you get to read in the
Changlog
:From what I understand, you could try any of the possible ones
["default", "cairo", "cairo-png", "quartz", "windows", "ragg"]
, for examplecairo
And indeed, you can configure
cairo
com renderAnd it greatly improves the quality of straight lines.