drbunsen
drbunsen

Reputation: 10689

How to produce a tiled plot with one variable with ggplot?

I am trying to make a tiled plot with ggplot2 with a single variable. The data looks like this:

fruit numbers
apples 6
peaches 2
grapes 10
cherries 2
... many more fruit

I can plot the same data as the x and as the y variables, I can use this code:

p <- qplot(fruit,fruit) + geom_tile(aes(fill=numbers))

The output looks like this:

enter image description here

The above plot is precisely what I would like to plot; however, I need the data arranged in a single horizontal 1-dimensional plot. How can I create a 1D horizontal titled plot with 1 variable?

Upvotes: 1

Views: 1531

Answers (2)

Richie Cotton
Richie Cotton

Reputation: 121077

Dataset (guessed the interpretation of the data). The trick is to include a dummy y variable.

pacman_data <- data.frame(
    fruit = c("apples", "peaches", "grapes", "cherries"),
    numbers = c(6, 2, 10, 2),
    dummy = 1
)

Here's the plot. You can use geom_tile or geom_bar, the only difference seems to be that geom_bar gives you a small gap between the tiles by default.

p_base <- ggplot(pacman_data, aes(fruit, dummy, fill = numbers)) +
    ylab("") +
    opts(axis.text.y = theme_blank())
p_base + geom_tile()
p_base + geom_bar()

Upvotes: 2

James
James

Reputation: 66834

p <- qplot(fruit," ") + geom_tile(aes(fill=numbers))

Upvotes: 5

Related Questions