Ich möchte eine Funktion zum Speichern von Plots erstellen (von ggplot
). Ich habe viele solche Plots, so dass dies mir helfen wird, effektiver zu arbeiten.Funktion zum Speichern von ggplot
Hier ist ein Datenrahmen:
### creating data frame
music <- c("Blues", "Hip-hop", "Jazz", "Metal", "Rock")
number <- c(8, 7, 4, 6, 11)
df.music <- data.frame(music, number)
colnames(df.music) <- c("Music", "Amount")
Dann erstelle ich eine Handlung:
### creating bar graph (this part is OK)
myplot <- ggplot(data=df.music, aes(x=music, y=number)) +
geom_bar(stat="identity") +
xlab(colnames(df.music)[1]) +
ylab(colnames(df.music)[2]) +
ylim(c(0,11)) +
ggtitle("Ulubiony typ muzyki wśród studentów")
Jetzt möchte ich diesen Plan zu .pdf
speichern.
Dies funktioniert:
pdf("Myplot.pdf", width=5, height=5)
plot.music.bad
dev.off()
Allerdings würde ich dies mit einer Funktion automatisieren möchten, die als Argument den Plot ich speichern möchten. Ich weiß nicht genau, wie es geht; hier ist was ich versucht habe:
save <- function(myplot){
plot<- myplot
pdf("lol.pdf", width=5, height=5)
plot
dev.off()
}
### .pdf file is created but doesn't work
save(myplot)
Also, wie kann ich es tun? Danke für Ihre Hilfe. Justyna
hast du 'ggsave' versucht? –