Reputation: 175
I'd like to create a ggplot2 plot with the bars being on the negative side and the positive side of the x axis like so:
df <- data.frame(
var1 = c("a", "b", "c", "d", "e", "f", "g"),
var2 = c(-3, -2, -1, 0, 1, 2, 3)
)
ggplot(df, aes(x=var1, y=var2, fill=var1)) +
geom_bar(stat="identity", position="dodge") +
guides(fill=FALSE)
with the slight difference that the values are all positive. as an example we add 10 to each data point in df$var2
and now want to display the relative change around the mean of all values - value 10. the plot should look the same with the exception of the values for the y axis.
df2 <- df
df2$var2 <- df2$var2 + 10
How can I plot df2 to look like df? I don't like to change the labels on the y axis using breaks=
and labels=
as I have many different plots with ever changing values.
Any help is highly apprechiated!
EDIT: To comment on @alistaire 's concerns: I generally agree, nevertheless, I think there are circumstances where such a graph is suited and not necessarily misleading. If - for instances - I like to display the relative enrichment or depletion around a mean, but at the same time want to convey the information what the actual mean is, than I think such a graph can be quite helpful.
For example: See this figure in Nature Genetics (doi:10.1038/ng.3286)
Legend(a): Percentage of fragments containing GWAS SNPs that interact with a promoter, segregated by individual disease or trait subclass. The histogram shows the relative enrichment or depletion of each subclass, as compared to the total catalog of 8,808 SNPs analyzed, with data taken from 1,651 independent studies. The average association across all SNPs is shown as a percentage.
Upvotes: 0
Views: 3336
Reputation: 31
I have the same problem (legitimate imho : I want to plot seasonal ratios with an axis at 1 (neutral element…))
Using the barplot
function in the graphics
package
barplot(df$var2-10,offset=10)
Works fine for me.
Upvotes: 0
Reputation: 13108
To add to @alistaire's comment, with which I agree, you could also try using different geoms for the actual values and the differences, for example something like
ggplot(df2, aes(x = var1, y = var2)) +
geom_bar(stat = "identity", fill = "grey70") +
geom_errorbar(aes(ymin = pmin(var2, mean(var2)),
ymax = pmax(var2, mean(var2)),
colour = var1), size = 1) +
guides(colour = FALSE)
which would give you a plot like
Upvotes: 1