简体   繁体   中英

Using R to create an image don't want the white border or the title to appear only the PNG image

I'm using the code below to try to create a PNG image that is only the image but there is still some text and a white border.

theplot <- data  %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
  theme_void() + theme(legend.position="none") + theme(axis.title = element_blank())

I've also tried the following.

theplot <- data  %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
  theme_void() + theme(axis.line=element_blank(),
  axis.text.x=element_blank(),
  axis.text.y=element_blank(),
  axis.ticks=element_blank(),
  axis.title.x=element_blank(),
  axis.title.y=element_blank(),
  legend.position="none",
  panel.background=element_blank(),
  panel.border=element_blank(),
  panel.grid.major=element_blank(),
  panel.grid.minor=element_blank(),
  plot.background=element_blank())

I'm not very familiar with R so I'm not sure if ggplot is what I should be using to create just an image.

Is it ok like this ?

library(ggplot2)
library(ggthemes)

ggplot(mtcars, aes(mpg, wt)) + geom_point() + 
  theme(plot.margin=unit(c(0,0,0,0), unit="mm")) 
  + theme_fivethirtyeight()

ggsave("myplot.png")

在此处输入图片说明

One can take a look at the code of theme_fivethirtyeight :

> theme_fivethirtyeight
function (base_size = 12, base_family = "sans") 
{
    (theme_foundation(base_size = base_size, base_family = base_family) + 
        theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"], 
            linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]), 
            axis.title = element_blank(), axis.text = element_text(), 
            axis.ticks = element_blank(), axis.line = element_blank(), 
            legend.background = element_rect(), legend.position = "bottom", 
            legend.direction = "horizontal", legend.box = "vertical", 
            panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]), 
            panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0, 
                size = rel(1.5), face = "bold"), plot.margin = unit(c(1, 
                1, 1, 1), "lines"), strip.background = element_rect()))
}

I must say I don't understand why that doesn't work with your custom theme. I don't see a major difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM