How can I draw this graph using iris data?


Special case agency

Using the iris dataset

I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66

I have two questions:

1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (right) we can still see the lines. How can I draw the graph in such a way that the first and second graphs look exactly the same?

2.) How to draw the same diagram using grid viewport tree like viewport(), pushViewport() and upViewport()

Here is my sample code:

library(ggplot2)
library(readr)
library(grid)
library(gridBase)

 head(x = iris)

  a <- ggplot(data = iris,
   aes(x=Sepal.Length, y=Sepal.Width)) + 
    geom_point()



  b <- ggplot(data = iris,
   aes(x = Sepal.Length)) + 
    geom_histogram()


  c <- ggplot(data = iris,
      aes(x = Sepal.Width)) + 
      geom_histogram() +
      coord_flip()


   # Put these graphs into one

      grid.newpage()
      pushViewport(viewport(layout = grid.layout(2, 2)))
      vplayout <- function(x, y) viewport(layout.pos.row = x, 
                                layout.pos.col = y)
      print(b, vp = vplayout(1, 1))  # key is to define vplayout
      print(a, vp = vplayout(2, 1)) 
      print(c, vp = vplayout(2, 2))

      sample_vp <- viewport(width = 0.66, height = 0.66)

      pushViewport(sample_vp)

Thank you in advance

my output:

enter image description here

Expected output:

enter image description here

Trumbrand

For 1), yes, the lines are ugly. I don't know what's causing it, but since each bar is a rectangle, I think it must be a graphical glitch. You can avoid this by setting the color of the histogram to be the same as the fill.

For 2), I'm going to play the adventure game, i.e. instead of giving the answer you want to hear (ie how to do these things in the grid), I'll give you an answer that I think you need to hear.

Assuming your goal is to display these histograms as marginal histograms on the main panel, you can easily achieve something similar in patchwork.

library(ggplot2)
library(patchwork)
#> Warning: package 'patchwork' was built under R version 3.6.3

a <- ggplot(data = iris,
            aes(x=Sepal.Length, y=Sepal.Width)) + 
  geom_point()

b <- ggplot(data = iris,
            aes(x = Sepal.Length)) + 
  geom_histogram()


c <- ggplot(data = iris,
            aes(x = Sepal.Width)) + 
  geom_histogram() +
  coord_flip()

b + plot_spacer() + a + c + 
  plot_layout(nrow = 2, ncol = 2, widths = c(1, 0.5), heights = c(0.5, 1))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2020-04-18 by the reprex package (v0.3.0)

You can also ignore axes for edge plots by setting breakpoints and names:

b + scale_x_continuous(breaks = NULL, name = "") + 
  plot_spacer() +
  a + 
  c + scale_x_continuous(breaks = NULL, name = "") +
  plot_layout(nrow = 2, ncol = 2, widths = c(1, 0.5), heights = c(0.5, 1))

Related


How can I draw this graph using iris data?

Special case agency Using the iris dataset I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66 I have two questions: 1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (ri

How can I draw this graph using iris data?

Special case agency Using the iris dataset I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66 I have two questions: 1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (ri

How can I draw this graph using iris data?

Special case agency Using the iris dataset I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66 I have two questions: 1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (ri

How can I draw this graph using iris data?

Special case agency Using the iris dataset I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66 I have two questions: 1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (ri

How can I draw this graph using iris data?

Special case agency Using the iris dataset I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66 I have two questions: 1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (ri

How can I draw this graph using iris data?

Special case agency Using the iris dataset I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66 I have two questions: 1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (ri

How can I draw this graph using iris data?

Special case agency Using the iris dataset I want to draw a graph like this: Using viewport(), the scatter plot has a width and height of 0.66 I have two questions: 1.) As you can see in the second plot (right) the lines are smoother, but in the first plot (ri

how can i draw graph in spring mvc in jsp using highchart

Francisco Gallardo: i want to draw graph in jsp unsando spring mvc and highchart but i still can't do it, i wait for your answer thanks My point, this is where I want to draw the graph, but the graph is not showing, it shows nothing <%@ taglib uri="http://java

How can I draw an energy ranking graph using Python?

twin penguins This is a typical energy ranking used in some published papers, and I am struggling to reproduce one for my data using Python (any matplotlib, sns, etc.). I structure the data in a Pandas dataframe like this: System Color_id Data1 Data2 Form I

how can i draw graph in spring mvc in jsp using highchart

Francisco Gallardo: i want to draw graph in jsp unsando spring mvc and highchart but i still can't do it, i wait for your answer thanks My point, this is where I want to draw the graph, but the graph is not showing, it shows nothing <%@ taglib uri="http://java

How can I draw an energy ranking graph using Python?

twin penguins This is a typical energy ranking used in some published papers, and I am struggling to reproduce one for my data using Python (any matplotlib, sns, etc.). I structure the data in a Pandas dataframe like this: System Color_id Data1 Data2 Form I

How can I draw this graph in Android using XML?

João Correia I'm trying to draw a form in an image in Android XML. But so far my attempts have been unsuccessful. what should I do? Thanks in advance place If you use RelativeLayout, you can make a black rectangle and put a white triangle image on top. I can't

how can i draw graph in spring mvc in jsp using highchart

Francisco Gallardo: i want to draw graph in jsp unsando spring mvc and highchart but i still can't do it, i wait for your answer thanks My point, this is where I want to draw the graph, but the graph is not showing, it shows nothing <%@ taglib uri="http://java

how can i draw graph in spring mvc in jsp using highchart

Francisco Gallardo: i want to draw graph in jsp unsando spring mvc and highchart but i still can't do it, i wait for your answer thanks My point, this is where I want to draw the graph, but the graph is not showing, it shows nothing <%@ taglib uri="http://java

How can I draw an energy ranking graph using Python?

twin penguins This is a typical energy ranking used in some published papers, and I am struggling to reproduce one for my data using Python (any matplotlib, sns, etc.). I structure the data in a Pandas dataframe like this: System Color_id Data1 Data2 Form I

How can I draw this graph in Android using XML?

João Correia I'm trying to draw a form in an image in Android XML. But so far my attempts have been unsuccessful. what should I do? Thanks in advance place If you use RelativeLayout, you can make a black rectangle and put a white triangle image on top. I can't

How can I draw such a graph in Python?

Santos I want to draw a graph like the image below: I wrote the following code to plot the waveform for each time step. import matplotlib.pyplot as plt import numpy as np def u_0(x): a = 1.0/np.cosh(2.0*(x+8.0)) b = 1.0/np.cosh((x+1.0)) return 8.0

How can I draw this graph in Excel?

Pablo I have a CSV file with the following data This series has 500 rows. However, I need to plot whatever is on column A as the horizontal axis and whatever is on column B [0..499] as the vertical axis. I would like to label the horizontal axis values at ever

How can I draw a complete graph in R?

Mrsteve I want to draw complete graphs in R. How can I do this? I've only found one package on CRAN that has the ability to generate a full graph. But this package (i.e. "RnavGraph") is not installed but exits with error status. Further searching seems to be d

How can I draw this graph in R?

cooldood3490 I want to draw speedVS. Data are as follows:age Speed Age 15 <18 30 18-25 40 26-40 32 40+ How can I plot this as a scatterplot in R? I don't know how to do range. So far this is what I have. speed<-c(15,30,40,32) Peter Accu

How can I draw such a graph in Python?

Santos I want to draw a graph like the image below: I wrote the following code to plot the waveform for each time step. import matplotlib.pyplot as plt import numpy as np def u_0(x): a = 1.0/np.cosh(2.0*(x+8.0)) b = 1.0/np.cosh((x+1.0)) return 8.0

How can I draw this graph in Excel?

Pablo I have a CSV file with the following data This series has 500 rows. However, I need to plot whatever is on column A as the horizontal axis and whatever is on column B [0..499] as the vertical axis. I would like to label the horizontal axis values at ever

How can I draw this graph in R?

cooldood3490 I want to draw speedVS. Data are as follows:age Speed Age 15 <18 30 18-25 40 26-40 32 40+ How can I plot this as a scatterplot in R? I don't know how to do range. So far this is what I have. speed<-c(15,30,40,32) Peter Accu

I can't draw this graph

Dana Cassio Costa I've been trying to draw this graph all night. Please, can someone help me? Here's what I've done so far. The constant function is in the middle of the plot, not sure why, I can't add smaller ticks to the bottom and top of the plot. theta = c

I can't draw this graph

Dana Cassio Costa I've been trying to draw this graph all night. Please, can someone help me? Here's what I've done so far. The constant function is in the middle of the plot, not sure why, I can't add smaller ticks to the bottom and top of the plot. theta = c

I can't draw this graph

Dana Cassio Costa I've been trying to draw this graph all night. Please, can someone help me? Here's what I've done so far. The constant function is in the middle of the plot, not sure why, I can't add smaller ticks to the bottom and top of the plot. theta = c

I can't draw this graph

Dana Cassio Costa I've been trying to draw this graph all night. Please, can someone help me? Here's what I've done so far. The constant function is in the middle of the plot, not sure why, I can't add smaller ticks to the bottom and top of the plot. theta = c

I can't draw this graph

Dana Cassio Costa I've been trying to draw this graph all night. Please, can someone help me? Here's what I've done so far. The constant function is in the middle of the plot, not sure why, I can't add smaller ticks to the bottom and top of the plot. theta = c

How can I draw a legend for this graph with many features?

LAD: I have a matrix with Xonly 1 row as input . I have a matrix Ywith each row as the output of a function. If Ythere are 5 lines, then we have 5 different functions. I use the following code to plot all functions on the same graph: import numpy as np import