facet ordering and then labeling x axis - r

i have this dataframe and I'd like to use scale_x_discrete to label the x axis
dat = data.frame(group= c("A","B","B"),y = c(1,2,3),row = c(1,1,2),LABEL = c("la","la","lb"))
ggplot(dat, aes(x= row, y = y))+geom_point()+facet_wrap(~group)+
scale_x_discrete(
breaks = row,
labels = LABEL
)
but I get an error: Error in check_breaks_labels(breaks, labels) : object 'LABEL' not found
any idea how to fix it?
Thank you.

If you want x to be discrete, you'll need to make it a factor with x = factor(row).
Then just reference the dat data frame explicitly in scale_x_discrete:
ggplot(dat, aes(x = factor(row), y = y)) +
geom_point() +
facet_wrap(~group) +
scale_x_discrete(breaks = dat$row, labels = dat$LABEL)
Alternately, you can keep x as continuous and use scale_x_continuous instead to relabel the ticks:
ggplot(dat, aes(x = row, y = y)) +
geom_point() +
facet_wrap(~group) +
scale_x_continuous(breaks = dat$row, labels = dat$LABEL)

Related

How to correctly specify a column as the fill colour in geom_ribbon?

I can't seem to be able to set different fill colours for geom_ribbon(), using one of the columns as input to fill
library(ggplot2)
time <- as.factor(c('A','B','C','D'))
grouping <- as.factor(c('GROUP1','GROUP1','GROUP1','GROUP1',
'GROUP2','GROUP2','GROUP2','GROUP2'))
x <- c(1.00,1.03,1.03,1.06,0.5,0.43,0.2,0.1)
x.upper <- x+0.05
x.lower <- x-0.05
df <- data.frame(time, x, x.upper, x.lower,grouping)
ggplot(data = df,aes(as.numeric(time),x,group=grouping,color=grouping)) +
geom_ribbon(data = df, aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower),
fill=grouping, alpha=.5) +
geom_point() + labs(title="My ribbon plot",x="Time",y="Value") +
scale_x_continuous(breaks = 1:4, labels = levels(df$time))
I get the error Error: Unknown colour name: grouping but fill=c("pink","blue") works fine. I don't want to specify the colours manually.
All other examples I can find simply list the column in the fill argument so I'm not sure what I'm doing incorrectly.
Move fill = grouping inside aes so that this column is mapped to the fill variable.
ggplot(data = df, aes(as.numeric(time), x, color = grouping)) +
geom_ribbon(data = df, aes(ymax = x.upper, ymin = x.lower,
fill = grouping), alpha = 0.5) +
geom_point() +
labs(title = "My ribbon plot", x = "Time", y = "Value") +
scale_x_continuous(breaks = 1:4, labels = levels(df$time))

How to start a ggplot with a negative x axis value when data do not contain negative value?

I have some labels in the data that I want to annotate on the plot. I am trying to use directlabels package for annotation. Following is what I get:
library(directlabels)
ggplot(data =appr_CF_nlc %>% filter(SP_AB_Gp==7),
aes(x = frame_index, y = LV_frspacing.m, color = label, group=label)) + geom_line() +
geom_dl(aes(label = label), method = list(dl.trans(x = x+.2), "first.points"))
You can see that labels are not completely visible. How can I fix this? I have tried using scale_x_continuous with no luck:
ggplot(data =appr_CF_nlc %>% filter(SP_AB_Gp==7),
aes(x = frame_index, y = LV_frspacing.m, color = label, group=label)) + geom_line() +
geom_dl(aes(label = label), method = list(dl.trans(x = x + 0.), "first.points")) +
scale_x_continuous(expand=c(0.5,0), limits=c(0, 3500))

Setting x-axis breaks to the negative of the x aesthetics

I would like to make a plot where the breaks along the x-axis are the negative of the actual values I plot. Something like this
df <- tibble(x = seq(-1000, 0, length.out = 100),
y = 2 * x + 3)
ggplot(df) +
geom_line(aes(x = x, y = y)) +
scale_x_continuous(breaks = df$x, labels = -df$x)
except that this puts a break at every x value and I would like the breaks to be what I would get using waiver(). I recall seeing a solution for this, but for the life of me I cannot remember what it is.
ggplot(df) +
geom_line(aes(x = x, y = y)) +
scale_x_continuous(breaks = pretty(df$x), labels = -pretty(df$x))
pretty is all you need.
Alternatively, just plot directly and use a reversed scale:
ggplot(df) +
geom_line(aes(x = -x, y = y)) +
scale_x_reverse()

Dynamically formatting individual axis labels in ggplot2

This may end up being an expression or call question, but I am trying to conditionally format individual axis labels.
In the following example, I'd like to selectively bold one of the axis labels:
library(ggplot2)
data <- data.frame(labs = c("Oranges", "Apples", "Cucumbers"), counts = c(5, 10, 12))
ggplot(data = data) +
geom_bar(aes(x = labs, y = counts), stat="identity")`
There is similar problem here, but the solution involves theme and element_text. I am trying to use axis labels directly.
I can do this manually as below:
breaks <- levels(data$labs)
labels <- breaks
labels[2] <- expression(bold("Cucumbers"))
ggplot(data = data) +
geom_bar(aes(x = labs, y = counts), stat="identity") +
scale_x_discrete(label = labels, breaks = breaks)
But, if I try to do it by indexing instead of typing out "Cucumbers", I get the following error:
breaks <- levels(data$labs)
labels <- breaks
labels[2] <- expression(bold(labels[2]))
ggplot(data = data) +
geom_bar(aes(x = labs, y = counts), stat="identity") +
scale_x_discrete(label = labels, breaks = breaks)
Which makes sense, because it is not evaluating the labels[2]. But, does anyone know how to force it to do that? Thanks.
How about
breaks <- levels(data$labs)
labels <- as.expression(breaks)
labels[[2]] <- bquote(bold(.(labels[[2]])))
ggplot(data = data) +
geom_bar(aes(x = labs, y = counts), stat="identity") +
scale_x_discrete(label = labels, breaks = breaks)
Here we are more explicit about the conversion to expression and we use bquote() to insert the value of the label into the expression itself.
Another option is to set the font face dynamically with theme, though I'm not sure if this is in any sense a better or worse method than #MrFlick's answer:
breaks <- levels(data$labs)
# Reference breaks by name
toBold = "Cucumbers"
ggplot(data = data) +
geom_bar(aes(x = labs, y = counts), stat="identity") +
scale_x_discrete(label = labels, breaks = breaks) +
theme(axis.text.x=
element_text(face=ifelse(breaks %in% toBold, "bold", "plain")))
# Reference breaks by position
label.index=2
ggplot(data = data) +
geom_bar(aes(x = labs, y = counts), stat="identity") +
scale_x_discrete(label = labels, breaks = breaks) +
theme(axis.text.x=
element_text(face=ifelse(breaks %in% labels[match(label.index, 1:length(breaks))],
"bold", "plain")))

Edit the livery of the legend in a geom_area plot

How to add livery for the legend in geom_area? I tried something but it does not work.
time<-as.POSIXlt(c("2013-07-01","2013-07-01","2013-07-02","2013-07-02"),origin = "1960-01-01",tz="GMT")
data<-data.frame(xAxis=time,yAxis=c(3,2,1,2),split=factor(c(1,2,1,2)))
p<-ggplot(data,aes(x=xAxis,y=yAxis,fill=split))
p<-p + geom_area(stat="identity")
#p <- p + scale_color_discrete(name ="Name", labels=LETTERS[1:2])
p <- p + xlab("x-Axis") + ylab("y-Axis")
p
I think you need a scale that better matches your aes in ggplot
ggplot(data, aes(x = xAxis, y = yAxis, fill = split)) +
geom_area(stat = "identity") +
scale_fill_discrete(name = "Name", labels = LETTERS[1:2])
If you are going to use 'split' repeatedly and always want to have the same labels, you might consider re-labelling the factor before you start plotting (or whenever informative labels of a factor is relevant, e.g. modelling).
data$split2 <- factor(data$split, labels = LETTERS[1:2])
# no need for the 'labels' argument in scale
ggplot(data, aes(x = xAxis, y = yAxis, fill = split2)) +
geom_area(stat = "identity") +
scale_fill_discrete(name = "Name")

Resources