Mike
Mike

Reputation: 9

ggplot2 plots more points than asked

I am trying to fill a square region with non-overlapping squares with different colors and ggplot2 is plotting more points than those in the dataframe at the higher x and y limits. Here is the code

l = 1000
a=seq(0,1, 1/(l-1))
x=rep(a, each=length(a))
y=rep(a, length(a))

k = length(x)
c=sample(1:10, k, replace = TRUE)

data <- data.frame(x, y, c)

ggplot(data, aes(x=x, y=y)) + geom_point(shape=15, color=c)

ggsave('k.jpg', width=10, height=10)

The result I am getting with RStudio is this. Notice the extra points on the right and top of the image.

enter image description here

How can I get ggplot to plot exactly one square exclusively for those points in the dataframe and not more?

As a second related question, this is what happens if l is changed from 1000 to l=100

enter image description here

My problem is now that the squares are not perfectly stacked, leaving empty space between them. I would like to know how can I compute from the number of points in each dimension of the array (l), the correct value for size inside geom_point so that the squares are perfectly stacked.

Many thanks

Upvotes: 0

Views: 115

Answers (2)

mpalanco
mpalanco

Reputation: 13570

Providing a couple of alternatives using OP's example, reducing the data frame dimension to increase the size of the tile:

Data

library(ggplot2)
l = 100
a = seq(0, 1, 1 / (l - 1))
x = rep(a, each = length(a))
y = rep(a, length(a))
k = length(x)
c = sample(1:10, k, replace = TRUE)
data <- data.frame(x, y, c)

Example 1

Very simple, just pasing "white" as colour to make the tiles more distinctive.

ggplot(data, aes(x = x, y = y, fill = c)) + geom_tile(colour = "white")

enter image description here

Example 2

Creating manually a palette, and coord_equal to force a specified ratio (default 1) so tiles are squares:

colors<-c("peachpuff", "yellow", "orange", "orangered", "red", 
          "darkred","firebrick", "royalblue", "darkslategrey", "black")
ggplot(data, aes(x = x, y = y)) +
  geom_tile(aes(fill = factor(c)), colour = "white") +
  scale_fill_manual(values = colors, name = "Colours") +
  coord_equal()

enter image description here

Comparing geom_point and geom_tile

Creating small data frame (10 x 10, l = 10) to observe closer what happens when using geom_point instead of geom_tile.

Original OP code

ggplot(data, aes(x = x, y = y)) + geom_point(shape = 15, color = c)

enter image description here

Example 1

ggplot(data, ae(x = x, y = y, fill = c)) + geom_tile(colour = "white")

enter image description here

Example 2

colors<-c("peachpuff", "yellow", "orange", "orangered", "red", 
          "darkred","firebrick", "royalblue", "darkslategrey", "black")
ggplot(data, aes(x = x, y = y)) +
  geom_tile(aes(fill = factor(c)), colour = "white") +
  scale_fill_manual(values = colors, name = "Colours") +
  coord_equal()

enter image description here

Upvotes: 0

Andrew Gustar
Andrew Gustar

Reputation: 18425

You might be better off with geom_tile, rather than geom_point, as this will allow more control over the size of the rectangles and the border width. See ?geom_tile for details.

Upvotes: 1

Related Questions