`ggsave` prints a graph which is different from the original one

33 views Asked by At

I am using ggplot with R using the GUI RStudio. I am facing a strange issue, it looks like a graph made with ggplot plotted on the graphical part of RStudio gets printed on a different way when saved with ggsave.

Here I report the example code:

##Generation of the dataframe
Textile <- data.frame(
  Product = c("A", "B", "C", "D", "E"),
  Vix = c(32000, 20000, 30000, 17000, 30000),
  OT = c(2.5, 7, 17.5, 11, 5),
  Otbis = c("2-3", "6-8", "13-15", "10-12", "4-6"),
  Vixbis = c("27-37000", "15-25000", "25-35000", "14-20000", "20-30000")
)

View(Textile)
Textile


##Libraries loading
library(tidyverse)
library(ggplot2)
library(ggrepel)



##Variales creation
Product<-Textile[[1]]
Vix<-Textile[[2]]
OT<-Textile[[3]]

###Redefinition name first Row
row.names(Textile) <- Product


### Theme defined
theme_set(
  theme_bw() +
    theme(legend.position = "top")
  )




##Graph creation, I named it nbaplot
nbaplot <- ggplot(Textile, aes(x= OT, y = Vix, label = Product)) +   
geom_point(color = "red", size = 2) +  
xlab("X-axis") + 
ylab("Y-axis") +
geom_text_repel(max.overlaps = Inf, segment.curvature = 0.1, segment.ncp = 3, segment.angle = 60, force = 10,   size = 2.5, angle = 0, segment.alpha = 0.25, segment.size = 0.15,  nudge_y = 0.5, segment.square =FALSE, min.segment.length = 1, family = "Calibri", box.padding = 2.5, point.padding = 2.5, aes(label=paste0(Product, "\n", "Viscosity: ", Vixbis, " mPa s", "\n", "Defined time: ", Otbis, " sec")),
    size = 3,
     hjust=1
  )

nbaplot

Which generates me this graph, on the RStudio windows, which is nice.

enter image description here

Then I try to export using the ggsave function and the positioning of the labels is different from the one created making the graph earlier on with nbaplot.

ggsave(filename="Grafico.png", plot=nbaplot, device="png", dpi=500) #Ti salva il grafico nella Working directory del progetto
ggsave(filename="GraficoWide.png", plot=nbaplot, device="png", dpi=500, width = 5197, height = 2864, units = "px") #Formato Wide

enter image description here enter image description here

How can I export the really generated nbaplot graph?

0

There are 0 answers