Ask Your Question
0

How can a scatterplot be created using R's ggplot, with each point occupying a square unit, regardless of the screen size?

asked 2022-01-08 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-10-24 01:00:00 +0000

ladyg gravatar image

To create a scatterplot using R's ggplot with each point occupying a square unit, regardless of the screen size you can use the following code:

library(ggplot2)

# create some sample data
x <- rnorm(100)
y <- rnorm(100)

# create a dataframe
data <- data.frame(x, y)

# create the plot
ggplot(data, aes(x = x, y = y)) +
  geom_point(shape = 15, size = 10)

The size parameter in geom_point() controls the size of the points on the plot. By default, this is specified in terms of millimeters, so the size of the points will vary depending on the screen size. However, you can specify the size of the points in terms of a fixed number of screen units using the unit() function from the grid package. For example, to specify that each point should occupy a square unit of screen space, regardless of the screen size, you can change the size parameter to unit(1, "cm"):

ggplot(data, aes(x = x, y = y)) +
  geom_point(shape = 15, size = unit(1, "cm"))

This will ensure that each point occupies a square of 1 cm x 1 cm on the screen. You can adjust the size of the squares by changing the size argument in the unit() function.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-01-08 11:00:00 +0000

Seen: 13 times

Last updated: Oct 24 '21