0

How to remove the cluttered x-axis labels and arrange them in the desired order?

# Data generation
dataPlot <- data.frame(iVal = sample(paste('i', 1:22, sep=''), 300, replace=TRUE), 
                       facetVal = sample(LETTERS[1:3], 300, replace = TRUE),
                       value = runif(300, 0, 1))

# Original Facetted plot
ggplot(dataPlot, aes(iVal, value)) + geom_point() + facet_wrap(~facetVal)

# Data structure
str(dataPlot)
# 'data.frame': 300 obs. of  3 variables:
#   $ iVal    : Factor w/ 22 levels "i1","i10","i11",..: 10 20 1 21 16 7 11 18 ...
# $ value   : num  0.2483 0.0298 0.5532 0.1117 0.0386 ...
# $ facetVal: Factor w/ 3 levels "A","B","C": 2 1 1 2 2 3 1 3 3 3 ...

enter image description here

# Data generation
dataPlot <- data.frame(iVal = sample(paste('i', 1:22, sep=''), 300, replace=TRUE), 
                       facetVal = sample(LETTERS[1:3], 300, replace = TRUE),
                       value = runif(300, 0, 1), stringsAsFactors = FALSE)

# Original Facetted plot
ggplot(dataPlot, aes(iVal, value)) + geom_point() + facet_wrap(~facetVal)

enter image description here

How can manual breaks be set in this case (without hand-picking the labels to be seen) where the labels are not numeric(i1...i22) but the points are related in the same order(1:22)? I mean, instead of all 22 labels on the x-axis, a subset of them, maybe 10 labels with equal spacing and uncluttered axis.

For example, like the below x-axis labels (but character labels)

# Data generation
dataPlot <- data.frame(iVal = sample(1:22, 300, replace = TRUE), 
                       facetVal = sample(LETTERS[1:3], 300, replace = TRUE),
                       value = runif(300, 0, 1), stringsAsFactors = FALSE)

# Original Facetted plot
ggplot(dataPlot, aes(iVal, value)) + geom_point() + facet_wrap(~facetVal)

enter image description here

Prradep
  • 5,506
  • 5
  • 43
  • 84

1 Answers1

1

First suggestion - reorder factors, and define x-axis tics

# to reorder
dataPlot$iVal<- factor(dataPlot$iVal, 
levels = unique(dataPlot$iVal[order(as.numeric(gsub("i","", dataPlot$iVal)))]),
ordered = TRUE)

ggplot(dataPlot, aes(iVal, value)) + 
geom_point() + facet_wrap(~facetVal) + 
scale_x_discrete(breaks=paste('i',seq(1,max(as.numeric(gsub("i","", dataPlot$iVal))),4),sep=""))

Second suggestion - convert to numeric and paste character on plot labels.

dataPlot$Val<- as.numeric(gsub("i","", dataPlot$iVal))

ggplot(dataPlot, aes(Val, value)) + 
  geom_point() + facet_wrap(~facetVal) +
  scale_x_continuous(labels=function(x) paste0("i",x))
Jason
  • 581
  • 3
  • 8
  • Thanks @Jason for two approaches. First one, I could have no option to hand pick the labels. Second one, starts from `i0` where `i0` is not actually present. I actually prefer the second one to mend for expected output. – Prradep Jan 16 '18 at 21:45
  • If you want to remove `i0`, you can use `scale_x_continuous(limits=c(min(dataPlot$Val),max(dataPlot$Val)), breaks=seq(min(dataPlot$Val),max(dataPlot$Val),4), labels=function(x) paste0("i",x))` – Jason Jan 16 '18 at 21:53