I have data with thousands of observations and 3 columns. X, Y and facet factor. Can I limit number of observations to 10 for each facet grid by setting some parameter in ggplot
without managing my data?
m1 = d1[,c("ACCOUNT_NAME", "Calculated_Potential", "SEGMENT")]
m1 = m1[order(m1[,"Calculated_Potential"], decreasing = T),]
p <- ggplot(data=m1, aes(x=reorder(ACCOUNT_NAME, Calculated_Potential),
y=Calculated_Potential/10^6)) +
geom_bar(stat="identity") +
theme(axis.text.x = element_text(angle = -90, hjust = 0)) +
facet_grid(~ SEGMENT)
I would try something like:
reorder(ACCOUNT_NAME, Calculated_Potential)[1:10]
but it throws an error. Without it ggplot just plots all the observations.
Is there quick workaround or I need to manage each group manually?
Sample data:
m1 = data.frame(ACCOUNT_NAME = stringi::stri_rand_strings(100, 10),
Calculated_Potential = runif(100, 10^6, 10^7),
SEGMENT = round(runif(100, 0, 2)))
3 faced plots with all those observations. I'd like to limit them to have only top 10 for each (as like they are sorted).
Appr. 2
I'm a bit closer using this:
# get sorted values for each facet
u = unique(m1$SEGMENT)
seg = NULL
for (i in 1:length(u)) { #i=1
m = m1[which(m1$SEGMENT == u[i]),]
seg[[i]] = m[order(m[,"Calculated_Potential"], decreasing = T),]
}
seg = lapply(seg, function(x) x[1:10,])
p <- ggplot(data=data.table::rbindlist(seg), aes(x=reorder(ACCOUNT_NAME, Calculated_Potential), y=(Calculated_Potential/10^6))) +
geom_bar(stat="identity") + theme(axis.text.x = element_text(angle = -90, hjust = 0)) +
facet_grid(~ SEGMENT)
but now all the data is plotted 30 observations for each facet so there's probably only way to make 3 separate plots not making facets.