0

I am trying to plot a graph but the y-axis (the plot is flipped) is getting clipped.

Here is the pdf that my code generates
Here is the pdf that my code generates

The SS of the Table_all table
The SS of the Table_all table

Here is the MWE:

x <- read.table("Table_all.txt", sep = "\t", header = TRUE, 
stringsAsFactors=TRUE) 

x$Types <- factor(x$Types, levels = x$Types[order(x$Percentage)]) # To order

p <- ggplot(x, aes(x = Types, y = Percentage))+ theme_classic() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
panel.grid.minor = element_blank(),
axis.text.x=element_text(size=16, color = "black", family = "Linux 
Libertine"), 
axis.text.y=element_text(size=16, color = "black", family = "Linux 
Libertine"),
axis.line.y = element_line(color="black", size = 0.5)) + 
geom_col(fill = "steelblue") + coord_flip(expand = FALSE, clip = 'off') + 
geom_text(aes(label=sprintf("%0.1f %%", Percentage), fontface = "bold", 
family = "Linux Libertine"), vjust=0.5, hjust=-0.2, color="black", 
size=4.2)+
xlab("") + ylab("") + ylim(0,60)

ggsave(p, filename = "whatever.pdf", device = cairo_pdf, dpi = 300)

dput(x) # To form the table

structure(list(Types = structure(c(9L, 5L, 7L, 6L, 10L, 3L, 4L, 
8L, 2L, 1L, 11L), .Label = c("Account Management", "Allocation of 
Resources", 
"Audit Review And Analysis", "Authenticator Management", "Boundry 
Protection", 
"Identification & Authentication", "Least Functionality", "Least 
Privilege", 
"Others", "Physical Access Control", "Remote Access"), class = "factor"), 
Percentage = c(54.1, 13.4, 6, 5.1, 4, 3.7, 3.4, 2.9, 2.7, 
2.4, 2.3)), class = "data.frame", row.names = c(NA, -11L))

What I have tried:

  • I have tried scale_y_discrete(),
  • Manually setting size,
  • clip("off"), (which is still there but it changes nothing).
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Mr. Panda
  • 485
  • 3
  • 14
  • Welcome to SO could you please give us a minimal working example? By that I mean that it would help if you give us some example data for the `x` variable – Mr_Z Oct 29 '20 at 19:07
  • Thank you. Sorry I forgot that, I now edited my question. The content of the "x" is available in the 2nd link. – Mr. Panda Oct 29 '20 at 19:19
  • Could you please run `dput(x)` and add the output to your question? This way it is easier to start helping you, instead of manually type in the data from the screenshot. You can check https://meta.stackoverflow.com/questions/315885/what-is-the-correct-way-to-share-r-data-on-stack-overflow for more information. – Mr_Z Oct 29 '20 at 19:32
  • @Mr_Z I added the dput(x) output and you are right that, it should be definitely added when asking such a question. – Mr. Panda Oct 29 '20 at 21:03
  • You can expand the area of the plot by adding `theme(plot.margin=unit(c(1,2,1,1),"cm"))`. The second value is the right hand margin. See here: https://stackoverflow.com/q/10836843/6240490 – AndreasM Oct 30 '20 at 10:18
  • Editing margins as @AndreasM suggested solved my problem. – Mr. Panda Oct 30 '20 at 12:00

0 Answers0