ggplot axis custom order with duplicate labels - r

set.seed(357)
x <- data.frame(name = sample(letters, 10), val = runif(10), stringsAsFactors = F)
x[c(2,6),"name"] <- c("k","k")
ggplot(x, aes(x = name, y = val)) + theme_bw() + geom_bar(stat = "identity")
How can I plot the axis in the same order as x$name? (Yes, the k is duplicate, I want that to show up in the plot like this axis: c k g f o k s v t q)
In the past I used to do:
x$name <- factor(x$name, levels = x$name[order(x$val)], ordered = T)
wich doesn't work any more thanks to:
http://r.789695.n4.nabble.com/factors-with-non-unique-quot-duplicated-quot-levels-have-been-deprecated-since-2009-are-more-depreca-td4721481.html
This is no duplicate of: ggplot: order of factors with duplicate levels
His data structure is completely different.
Also, I have tried setting limits in x_scale_discrete. Doesn't work.

Try this...
x$name2 <- 1:nrow(x)
ggplot(x, aes(x = factor(name2), y = val)) + theme_bw() + geom_bar(stat = "identity") +
scale_x_discrete(labels=x$name)

Actually, simply add the following setting xlab(x$name)
ggplot(x, aes(x = name, y = val)) + theme_bw() + geom_bar(stat = "identity") + xlab(x$name)

Related

Make geom_histogram display x-axis labels as integers instead of numerics

I have a data.frame that has counts for several groups:
set.seed(1)
df <- data.frame(group = sample(c("a","b"),200,replace = T),
n = round(runif(200,1,2)))
df$n <- as.integer(df$n)
And I'm trying to display a histogram of df$n, facetted by the group using ggplot2's geom_histogram:
library(ggplot2)
ggplot(data = df, aes(x = n)) + geom_histogram() + facet_grid(~group) + theme_minimal()
Any idea how to get ggplot2 to label the x-axis ticks with the integers the histogram is summarizing rather than the numeric values it is currently showing?
You could tweak this by the binwidth argument of geom_histogram:
library(ggplot2)
ggplot(data = df, aes(x = n)) +
geom_histogram(binwidth = 0.5) +
facet_grid(~group) +
theme_minimal()
Another example:
set.seed(1)
df <- data.frame(group = sample(c("a","b"),200,replace = T),
n = round(runif(200,1,5)))
library(ggplot2)
ggplot(data = df, aes(x = n)) +
geom_histogram(binwidth = 0.5) +
facet_grid(~group) +
theme_minimal()
You can manually specify the breaks with scale_x_continuous(breaks = seq(1, 2)). Alternatively, you can set the breaks and labels separately as well.

paste function for labeling in ggplot2

In the following plot, I want to rename x-axis by paste0 function.
daata <- data.frame(
q = paste0("q",1:20),
value = runif(n = 20, 2, 10))
ggplot2::ggplot(data = daata, aes(x = q, y = value)) +
geom_col()
so I used the following code:
q = paste0("q",1:20)
labels <- paste0("'", q,"'" , " = ", 1:20) %>% noquote()
# Or
labels <- noquote(paste0("'", q,"'" , " = ", 1:20))
ggplot2::ggplot(data = daata, aes(x = q, y = value)) +
geom_col() +
scale_x_discrete(labels = labels)
But it did not work. Why? (main question)
I want to search for solutions that make labels = c("'q1' = 1", ...) works.
Beside paste function I know two alternatives.
Using list:
labels = sapply(1:20, list)
names(labels) <- daata$q
ggplot2::ggplot(data = daata, aes(x = q, y = value)) +
geom_col() +
scale_x_discrete(labels = labels)
Using function:
ggplot2::ggplot(data = daata, aes(x = q, y = value)) +
geom_col() +
scale_x_discrete(labels = function(i){gsub("q", "", i)})
I am eager to know other solutions too.
How about something like this? Extract the question number in the data = step, and use that for the axis:
daata <- data.frame(
q = paste0("q",1:20),
value = 1:20)
ggplot2::ggplot(data = daata %>% mutate(order = str_remove(q, "q") %>% as.numeric),
aes(x = order, y = value)) +
geom_col() +
scale_x_continuous(breaks = 1:20, minor_breaks = NULL)
Edit: here's an alternative that extracts the numeric part of the label. As you'll note, this preserves the alphabetical ordering created by mapping x to q.
ggplot2::ggplot(data = daata, aes(x = q, y = value)) +
geom_col() +
scale_x_discrete(labels = function(x) parse_number(x))
why not giving a named vector?
labels <- parse_number(as.character(daata$q))
names(labels) <- as.character(daata$q)
p1 <- ggplot2::ggplot(data = daata, aes(x = q, y = value)) +
geom_col()
p2 <- p1 + scale_x_discrete(labels = labels)
cowplot::plot_grid(p1, p2, nrow = 1)

ggplot remove the redundant x axis values

library(ggplot2)
set.seed(2)
a = sort(rep(c("A","B"),6))
b = c(rep(1:3,2),rep(4:6,2))
cc = rnorm(length(a))
d = rep(sort(rep(1:2,3)),2)
df = data.frame(a,b,cc,d)
print(df)
ggplot(df, aes(x = as.factor(b), y = cc, fill = as.factor(d))) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~a)
In the following plot:
How do I get rid of the redundant x-axis values for each of the factors of a i.e. "A" & "B".
I mean the 4:6 are not required for "A" and similarly 1:3 for "B".
What is the tweak I need to do?
facet_wrap and facet_grid both have a scales argument that let you define which of the x and/ or y scales should be free or fixed.
In your case, you want the x dimensions to be free to be different in both facets, therefore
ggplot(df, aes(x = as.factor(b), y = cc, fill = as.factor(d))) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ a, scales = 'free_x')

R ggplot and factor(ordered): Why does my box plot gets ordered?

I want to draw a bar plot of this data.frame in the order it is at the moment:
df <- data.frame(y=rnorm(5),row.names=c("C","G","D","A","R"))
Height shall be y, x shall be the row names.
I tried tried the following with no success:
df$labels <- row.names(df)
ggplot(df, aes(x = labels, y = y)) + geom_bar(stat = "identity")
ggplot(df, aes(x = factor(labels, ordered = TRUE), y = y)) + geom_bar(stat = "identity")
df <- within(df, labels.factor <- factor(labels, levels=labels, ordered=T))
ggplot(df, aes(x = labels.factor, y = y)) + geom_bar(stat = "identity")
So my question is: Why does my "order" gets ignored? How do I do this correctly? I'm sure that I'm missing something obvious here as it is so basic. Thanks in advance.
Edit: I did a mistake in my R session and oversaw that one proposed solution actually worked. Thanks #jlhoward and user2633645.
Try:
df <- data.frame(cbind(x = c("C","G","D","A","R"), y=rnorm(5)), stringsAsFactors = FALSE)
head(df)
df$x <- factor(df$x, levels = c("C","G","D","A","R"))
levels(df$x)
class(df$y)
df$y <- as.numeric(df$y)
ggplot(df, aes(x = x, y = y)) + geom_bar(stat = "identity")

Fill being ignored with group + facet_wrap in ggplot2 / geom_bar

I suspect I might be using group incorrectly here, but I can't seem to understand why the fill color is getting ignored in the example below.
df <- data.frame(a = factor(c(1,1,2,2,1,2,1,2)),
b = factor(c(1,2,3,4,5,6,7,2)),
c = factor(c(1,2,1,2,1,2,1,2)))
p <- ggplot(df, aes(x=b)) +
geom_bar(aes(y = ..density.., group = c, fill=a), binwidth = 1) +
facet_wrap(~ c) +
scale_y_continuous(labels = percent_format()) +
scale_color_hue()
p
Any help would be greatly appreciated.
Thanks in advance,
--JT
I think I understand what plot you're after now. I'd do something like this:
df <- data.frame(a = c(1,1,2,2,1,2,1,2),
b = c(1,2,3,4,5,6,7,2),
c = c(1,2,1,2,1,2,1,2))
df <- within(df, { f <- 1 / ave(b, list(c), FUN=length)})
df[, 1:3] <- lapply(df[, 1:3], as.factor)
ggplot(df, aes(x = b)) + geom_bar(stat = "identity", position = "stack",
aes(y = f, group = c, fill = a), binwidth = 1) + facet_wrap(~ c) +
scale_y_continuous(labels = percent_format())
This gives the plot:

Resources