base R plot to ggplot

ggplot cheat sheet
convert plot to ggplot
ggplot histogram
r & ggplot2 tutorial
ggplot multiple plots
ggplotify
ggplot title
ggplot grob

I am trying to put a base R plot I have into ggplot format. The base R version looks great but I have a lot of white space and when I try to save to PDF it keeps the white space and doesn't fit in well in a document.

I like the plot the way it is but I would also like to add a title, annotations and label the x and y axis along with being able to apply a ggplot theme.

(I am happy to remain in base R for this but I have more familiarity with ggplot) - any help would be great in translating this plot into ggplot since the plot is created in a for loop I am not sure how this translates to a data frame suitable for plotting in ggplot.

plot(0,0,xlim=c(0,28),ylim=c(0,1),
     xaxt="n",yaxt="n",bty="n",xlab="",ylab="",type="n")
i <- 1
j = 1 
for(j in 1:7)
{
  test <- (6+j):13
  train <- (0+j):(5+j)
  arrows(0,1-j/20,15,1-j/20,0.05)

  x_dark <- seq(0,min(train)-1, by  = 1)
  y_dark <- rep(1-j/20,length(x_dark))
  points(x_dark,y_dark,pch = 19,col = "black")

  points(train,rep(1-j/20,length(train)),pch=19,col="blue")
  if(length(test) >= i)
    points(test[i], 1-j/20, pch=19, col="red")
  if(length(test) >= i)
    points(test[-i], rep(1-j/20,length(test)-1), pch=19, col="gray")
  else
    points(test, rep(1-j/20,length(test)), pch=19, col="gray")
}
text(17,.95," time")

The result:

An alternative way is to use a similar for loop than the one you use for building your base plot to build your dataframe as this:

y_dark <- NULL
y_blue <- NULL
y_red <- NULL
y_grey <- NULL

x_dark <- NULL
x_blue <- NULL
x_red <- NULL
x_grey <- NULL

for(x in 1:6)
{
  # Sequence for black points
  y_dark <- c(y_dark,1:x)
  x_dark <- c(x_dark,rep(x,x))

  # Sequence for blue points
  j <- x+1
  jmax <- j+6
  y_blue <- c(y_blue,j:jmax)
  x_blue <- c(x_blue, rep(x,length(j:jmax)))

  # Sequence for red points
  r <- jmax +1
  y_red <- c(y_red,r)
  x_red <- c(x_red, rep(x,length(r)))

  # sequence for grey points
  g <- r+1
  if(g > 14) 
  {

  }
  else
  {
    y_grey <- c(y_grey,g:14)
    x_grey <- c(x_grey, rep(x,length(g:14)))
  }
}

df_dark <- data.frame(x = x_dark, y = y_dark, color = "black")
df_blue <- data.frame(x = x_blue, y = y_blue, color = "blue")
df_red <- data.frame(x = x_red, y = y_red, color = "red")
df_grey <- data.frame(x = x_grey, y = y_grey, color = "grey")

And then, you can plot it using:

library(tidyverse)
DF <- bind_rows(df_dark, df_blue, df_red, df_grey)
DF_arrow <- data.frame(x = 1:6, x_end = 1:6,
                       y = rep(1,6), y_end = rep(15,6))
ggplot() +
  geom_segment(data = DF_arrow,
               aes(x = -x, xend = -x_end, y = y, yend = y_end),
               arrow = arrow(length = unit(0.03, "npc")))+
  geom_point(imherit.aes = FALSE, data = DF, aes(x = -x, y = y, color = color),
             size = 4)+
  coord_flip()+
  scale_color_identity()+
  theme(axis.text = element_blank(),
        axis.title = element_blank())+
  annotate(geom = "text", x = -1, y = 16, label = "time")

can i convert a base plot in r to a ggplot object?, No, I think unfortunately this is not possible. Even though this does not answer your real question, building it with ggplot is actually not difficult. In R base plot functions, the options lty and lwd are used to specify the line type and the line width, respectively. In ggplot2, the parameters linetype and size are used to decide the type and the size of lines, respectively. You will learn how to: Display easily the list of the different types line graphs present in R.

In ggplot you usually keep data for symbols of the same type in one data frame. Here you have dots and arrows, which means two data frames should be the ideal organization. Adding of the text is left as an exercise for the reader. I believe that the code is much more legible like this:

library(tidyverse)

tibble(y = 1:7, x = 1, xend = 16) %>% 
  mutate(y = -y, yend = y) ->
  darrows

expand.grid(x = 1:14, y = 1:7) %>% 
  mutate(color = case_when(
    x < y + 1 ~ "black",
    x < y + 7 ~ "blue",
    x < y + 8 ~ "red",
    T ~ "gray70"
  ),
  y = -y) %>% 
  ggplot(aes(x, y)) +
  geom_segment(aes(xend = xend, yend = yend), data = darrows,
               lineend = "butt", linejoin = "mitre", 
               arrow = arrow(length = unit(.1, "inches"), type = "closed")) +
  geom_point(aes(color = color), size = 3) +
  coord_equal() +
  scale_color_identity() +
  theme_void()

The result

How to make any plot in ggplot2?, Modify the aesthetics of an existing ggplot plot (including axis labels and color). To build a ggplot, we will use the following basic template that can be used for To use hexagonal binning with ggplot2 , first install the R package hexbin from� I would like to generate a figure that has a combination of base and ggplot graphics. The following code shows my figure using the base plotting functions of R: t &lt;- c(1:(24*14)) P &lt;- 24 A

Your data looks like it's born from a matrix, where all nodes in a square are used/defined.

There are many ways you could take "some data source" into a matrix like this. I just typed numbers quickly into excel and then copied it into R, resulting in this:

m <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 3L, 2L, 2L, 2L, 
2L, 2L, 4L, 4L, 3L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 3L, 2L, 2L, 2L, 
4L, 4L, 4L, 4L, 3L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 4L, 4L, 
4L, 4L, 4L, 4L, 3L), .Dim = c(7L, 14L))

While the next few lines convert these numbers to solid colors, the same effect could be done with ggplot2::scale_color_manual. After converting to literal colors, I convert into a 3-column frame using reshape2::melt (also works with data.table::melt, might work with tidyr:: funcs).

m[] <- c("black", "blue", "red", "gray")[m]
m[1:3, 1:3]
#      [,1]    [,2]    [,3]   
# [1,] "black" "blue"  "blue" 
# [2,] "black" "black" "blue" 
# [3,] "black" "black" "black"

d <- reshape2::melt(t(m))
head(d)
#   Var1 Var2 value
# 1    1    1 black
# 2    2    1  blue
# 3    3    1  blue
# 4    4    1  blue
# 5    5    1  blue
# 6    6    1  blue

From here:

d %>% 
  ggplot(aes(x = Var1, y = -Var2)) +
  geom_segment(data = arrows, aes(x = xmin, xend = xmax, yend = -Var2),
               arrow = arrow(length = unit(0.01, "npc"))) +
  geom_point(aes(color = value), size = 3) +
  scale_color_identity() +
  geom_text(data = data.frame(Var1 = ncol(m) + 2L, Var2 = 1, label = "time"),
            aes(label = label)) +
  labs(x = NULL, y = NULL) +
  theme_void()

Data visualization with ggplot2, ggplot2 is the most famous package for data visualization with R. This page offers tip Marginal plots are not natively supported by ggplot2 , but their realisation is how to control title main features: position, font, color, text and more. Basic� (Side note: When I make art in R, I exclusively use base R.) So anyway, on Tuesday I talked to our incoming graduate students about R, and presented some basics of data input and data visualization. I gave a simple example of base R vs ggplot2 using a histogram and then a scatter plot.

Data visualization with R and ggplot2, Learn how to use the ggplot2 package to create graphs in R--including the helper There is a helper function called qplot() (for quick plot) that can hide much of this Unlike base R graphs, the ggplot2 graphs are not effected by many of the� This function does its best attempt to take whatever you provide it and turn it into a grob. It is primarily meant to convert ggplot plots into grobs, but it will also take any grid object (grob), a recorded base R plot, a formula specifying a base R plot, a function that generates a base R plot, or a trellis object. as_grob (plot, device = NULL)

ggplot2 Graphs, ggplot2 Base graphics VS ggplot for more complex graphs: Base graphics colored scatter plot example: plot(Home.Value ~ Date, col = factor(State), data� Box plots. R base box plots: boxplot() Box plot with the number of observations: gplots::boxplot2() Read more —> Box Plots.

R graphics with ggplot2 workshop notes, This R graphics tutorial describes how to change line types in R for plots created using either the R base plotting functions or the ggplot2� R comes with built-in functionality for charts and graphs, typically referred to as base graphics. Then there are R packages that extend functionality. Although there are many packages, ggplot2 by Hadley Wickham is by far the most popular. These days, people tend to either go by way of base graphics or with ggplot2. It’s one or the other

Comments
  • plot(..., xlim=c(0,18), ylim=c(0.63,0.98)) removes the whitespace for me, but that seems too easy. Why are you explicitly preserving the whitespace with your x/y limits?
  • In ggplot you'll need two separate data frames, the dots and the arrows.