What\'s the best way to deal with too many factors in a plot, assuming that the factor variable is ordered? The default doesn\'t look nice:
ggplot(data.frame
Use different method of visualization - dotplot()
. You represent frequency by a single dot, and you move your factors to y axis to display it horizontally rather then vertically. This plus ordering gives you an easy visual indicator of frequency for each factor. It's a bit dense on the labels, but still shows you the factors if you zoom. Here is example with lattice
library(lattice)
d <- sort(table(factor(trunc(runif(10000, 0, 100)))))
dotplot(d, col=1, cex=0.5, scales = list(y = list(cex=0.5)))
But maybe what you want is something like factor frequency histogram, although I don't know what would you use it for. Just don't rotate x-axis labels, it makes it unreadable.
d <- factor(trunc(runif(10000, 0, 100)))
histogram(d, scales = list(x = list(at=seq(1,length(levels(dd)),5))))
You can flip the values.
ggplot(data.frame(x=factor(trunc(runif(10000, 0, 100)), ordered=T)), aes(x=x)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_histogram()
flip <- ggplot(data.frame(x=factor(trunc(runif(10000, 0, 100)), ordered=T)), aes(x=x)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_histogram()
If it's still too dense for your taste, you can set manual breaks. In this case, I use five.
prune <- ggplot(data.frame(x=factor(trunc(runif(10000, 0, 100)), ordered=T)), aes(x=x)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_x_discrete(breaks = seq(0, 100, by = 5)) +
geom_histogram()
library(gridExtra)
grid.arrange(flip, prune)