1

First, my code that generates a graphic using GGplot.

x <- c(1,1,1,1,1,1,2,2,2,2,2,2)
y <- c(0,0,0,0,0,0,0,0.33,0.17,0.16,0.14,0.2)
z<-data.frame(cbind(x,y))

client.name = "test"
client.year = 2015
score = 90

ggplot<-  
  ggplot(z,aes(x = x,y = y,fill = y))+
  geom_bar(stat = "identity", 
           fill = c("white","white","white","white","white","white",
          "white","#c00000","#ed7d31","#ffc000","#92d050","#00b050"),width = .3)+
  theme(legend.position = "none") +
  theme(axis.ticks = element_blank()) +
  theme(panel.grid = element_blank()) +
  theme(axis.title = element_blank()) +
  theme(axis.text = element_blank()) +
#   theme(panel.background = element_blank()) +
#   theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))+
  geom_segment(aes(x = 1.56, y = score/100, xend = 2.16, yend = score/100),size=1, arrow = arrow(angle = 20, length = unit(0.25, "inches"), ends = "first", type = "open"),linetype="solid")+ 
  annotate("text", x = 2, y = c(0.167,0.415,0.58,0.73,0.9), 
           label = c("Disparaging","Unhappy","Ambivalent","Happy","Delighted"), 
           colour="white", family="Calibri", fontface="bold") +
  annotate("text", x = 1, y = score/100, 
           label = paste(client.name,client.year, score, "%"),
           family="Calibri", fontface = "bold", hjust=-1) 
ggplot

The result, with my desired resize/trim

Plot with desired trim

I've looked for solutions about resizing the final product of a plot created in ggplot, but most answers pertain to re sizing the plotting area, which scales everything else. I've tried several values in theme(plot.margin = unit(c(0, 0, 0, 0), "cm")) to no avail. I assume I could output an image file of a particular size, but I need this to be a plot object in R that is output later directly to a document.

Is it possible to resize plots without affecting scaling by specifying the parts of the plot you want to keep? In MATLAB I'd go newPlot = oldPlot(400:end,400:end) or similar.

Many thanks!

EDIT*

Possible solution here, hacking the plot using grid. Yet to test (not at home until tomorrow). Will need to be a function of the client name string length still. Can I return the full coordinates of a string object on a plot (i.e. return xstart:xend, ystart:yend).

Community
  • 1
  • 1
Alex
  • 971
  • 4
  • 15
  • 28

0 Answers0