Home > Mobile >  Replicate graph using ggplot2 in R
Replicate graph using ggplot2 in R

Time:10-25

I have to replicate the following graph using ggplot2 in R enter image description here

This is my attempt:

library(dplyr)
library(scales)
d <- data.frame(x=c(4.5,9,5.5), y=c(3.5,2.5,8))
f <- data.frame(x=c(9,10,5.5), y=c(2.5,2.5,8))
df <- data.frame(x1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) , y1 = c(1, 4, 2.5, 4.5, 5.5, 8.5, 2.5, 1, 1.5, 2.5), x2 = 1:10, y2 = 1:10, x3 = c(2.1875, 5.8125, 6.25, 8.125, 9.3125), y3 = c(7.5, 6.375, 3, 3.875, 2.1875), x4 = c(1.3125, 1.5, 1.6875, 3.6875, 7.25), y4 = c(7.1875, 9.9375, 9.0625, 9.375, 3.8125))
grafico_4 <- ggplot(df)  
  ggtitle("Grafico variado")  
  theme(plot.title = element_text(size = 10))  
  theme(panel.background = element_rect(fill='white', colour='white'))  
  theme( axis.line = element_line(colour = "black", size = 0.5))  
  scale_y_discrete(limits = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))  
  scale_x_discrete(limits = factor(1:10), labels = c("uno", "", "tres", "", "cinco", "", "siete", "", "nueve", ""))  
  geom_polygon(data = d, mapping=aes(x = x, y = y), color = "white", alpha = 3, fill = "#AED9E6")  
  geom_polygon(data = f, mapping=aes(x = x, y = y), color = "white", alpha = 3, fill = "#AED9E6")  
  geom_hline(yintercept = 5, linetype = "dotted")  
  xlab("")  
  ylab("")  
  geom_line(aes(x = x1, y = y1), size = 1, alpha = 0.9, linetype = 2)   
  geom_point(aes(x = x2, y = y2), shape = 1)  
  geom_point(aes(x = x3, y = y3), shape = 1, color = "red")  
  geom_point(aes(x = x4, y = y4), shape = 3, color = "green", size = 0.8)  
  annotate(geom = "text", x = 1.7, y = 7.5, label = "cinco", color = "purple", size = 3)  
  annotate(geom = "text", x = 2, y = 6, label = "dos", color = "yellow", size = 3)  
  annotate(geom = "text", x = 6.625, y = 9.125, label = "cuatro", color = "blue", size = 3)  
  annotate(geom = "text", x = 7.875, y = 2, label = "tres", color = "green", size = 3)

which gives the following plot

enter image description here

Now, it is obvious that

  1. My dashed line looks wacky
  2. My x axis should be larger
  3. There's a white line running through my polygon

How can I fix my graph?

CodePudding user response:

  • Changing color="white" to color = "#AED9E6" in your polygons should take care of the white line, by having the edges drawn in the same color as the fill (or, specify the polygon with a single set of vertices rather than as the union of two triangles ...)
  • I'm not sure what you consider "wacky" about your dashed lines
  • the easiest way to change the aspect ratio and/or the relative size of text and features is to ggsave() with a specified height and width.

CodePudding user response:

The problem (for items 1 and 2) is not in your use of ggplot but rather in failing to set up your output device as you expect. It is the device that controls your aspect ratio. The jitteriness of your dashed lines is due to the low resolution of your device (which may be the default screen device for a computer with low resolution output). You can control that by increasing the "pixel density" (dpi). (I didn't need to increase the default for the png device because it is already creating a better rendition of the dashes but you can modify it if needed by using the parameters listed in the help page: ?png

png("test",width=700, height=480)
 grafico_4
 dev.off()

enter image description here

BB has already described how to fix the white line at the intersection.

  • Related