0

Here is my code. However, it outputs a very tall barplot and I want to shrink this VERTICALLY, not horizontally. I am a beginner in R, so any help would be great! Below is sample data.

SLC4A1domain <- read.csv(file.choose(), header=TRUE)

SLC4A1barplot <- barplot(as.matrix(SLC4A1domain), horiz=TRUE, xlab = "Length (Protein Domains Shown)",
        main = "Gene Data", col=c("azure", "plum1", "skyblue"), 
        legend = c("Cytoplasmic", "Helical Membrane", "Extracellular"))

Sample data, I imported from google sheets as a csv, one column.

structure(list(SLC4A1 = c(1209L, 72L, 24L, 0L, 63L, 0L, 9L, 51L, 
27L, 0L, 63L, 0L, 36L, 69L, 87L, 0L, 63L, 0L, 33L, 63L, 120L, 
0L, 63L, 0L, 48L, 57L, 0L, 0L, 54L, 0L, 69L, 63L, 0L, 0L, 57L, 
0L, 114L, 93L, 0L, 126L, 0L, 0L)), row.names = c(NA, 42L), class = "data.frame")

enter image description here

Josh
  • 23
  • 5
  • Hi, could you please share a [reproducible sample](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data? It's a little tough to check your code if we don't know what structure your CSV has – Punintended Aug 07 '19 at 16:39
  • @Punintended I edited the question with sample data – Josh Aug 07 '19 at 16:47
  • Sample data looks good, thanks! I don't understand your goal though. If you want to change the dimensions of the plot, you resize the plot window to whatever shape you want, and the plot will automatically adjust. This is almost always done manually, not in code. If you're saving the plot to an image file, then whatever save function you are using should have a way to specify the horizontal and vertical dimensions. – Gregor Thomas Aug 07 '19 at 16:49
  • Though, if you do want to open a plot window with specific dimensions you can - see the `?x11` help page. You'll need to use a device based on your OS (`x11()` for MacOS/Linux, `windows()` for windows. – Gregor Thomas Aug 07 '19 at 16:51
  • @Gregor Thank you for your help! However, I tried using the windows function, but it opened the plot in a new window, and therefore, I couldn't export the image as an SVG file. If I changed the dimensions in the export tab (shrinking it vertically), the legend would also shrink and would look squished and bad. Perhaps is there a way to move the legend off the barplot in my code. Once again, thank you so much for your help! – Josh Aug 07 '19 at 17:11

1 Answers1

0

If your goal is to output the plot as an SVG file, how about writing some code to do this, instead of outputting the plot manually?

The svglite package lets you export plots to SVG. You can then adjust the dimensions of the plot using the width and height arguments.

install.packages("svglite")
library(svglite)

svglite(file = "Rplots.svg", width = 10, height = 4)
barplot(as.matrix(SLC4A1domain), horiz=TRUE, xlab = "Length (Protein Domains Shown)",
        main = "Gene Data", col=c("azure", "plum1", "skyblue"), 
        legend = c("Cytoplasmic", "Helical Membrane", "Extracellular"))
dev.off()
Kelly
  • 43
  • 5