I’m having problems with ggplot using coordinate transformations with a log10 scale. I wish to plot my data on a log10 axis, but without scaling the data itself. This works with sqrt, but when using a log axis coordinates no bars appear. Please can you tell me what I’m missing?
d <- data.frame(x=factor(c(1,1,2,2)), y=c(1,2,3,4), fill=factor(c(1,2,3,4)))
#sqrt axis tranformaion works
ggplot(d, aes(x = x, fill = fill)) +
geom_bar(aes(y = y), stat = "identity", position = "dodge") +
coord_trans(y = "sqrt")
#log10 axis tranformaion doesn't work
ggplot(d, aes(x = x, fill = fill)) +
geom_bar(aes(y = y), stat = "identity", position = "dodge") +
coord_trans(y = "log10")
#log10 axis tranformaion works with points rather than bars
ggplot(d, aes(x = x, fill = fill)) +
geom_point(aes(y = y), stat = "identity") +
coord_trans(y = "log10")
Try using the scale_y_log10 function:
ggplot(d, aes(x = x, fill = fill)) +
geom_bar(aes(y = y), stat = "identity", position = "dodge") +
scale_y_log10()
Related
So I have this graph:
But I would like to add appropriate labels to the x axis so each column has a label. For this I used scale_x_discrete(limit = as.character(2008:2017)) however doing so gives this result:
As you can see the labels are all squashed to one side and the data bars to the other.
Am I doing something wrong? or is this bug?
Here is my code:
# First image
ggplot(data_melt, aes(x = year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.position = "none")
# Second image
ggplot(data_melt, aes(x = year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.position = "none") +
scale_x_discrete(limit = as.character(2008:2017))
Your labeling problem is due to the variable type and it has not nothing to do with the axis scale. Try changing the variable, in the x axis, from numerical to categorical:
data_melt$year <- as.factor(data_melt$year)
I am trying to create the following plot but with proportion on the y axis.
library(ggplot2)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
but when I add y=..prop.., it doesn't group it by clarity. I have tried the following:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., fill = clarity), position = "dodge")
To calculate proportion (or frequency) you can use ..count.. (proportion is specific count divided by all count's):
library(ggplot2)
ggplot(diamonds, aes(cut, (..count..) / sum(..count..), fill = clarity)) +
geom_bar(position = "dodge")
g = ggplot(Values, aes(x = X, y = Y, fill=factor(Z))) +
geom_bar(width=0.8, stat = "identity", position="dodge") +
facet_grid(Z ~ ., scale = "free_y") +
labs(x="Anno", y = "Riserva Rivalutata") +
theme(legend.position ="none") +
scale_x_continuous(breaks = seq(2000, 2070, by = 5))
d = ggplot(Values, aes(x = X, y = Y, fill=factor(Z))) +
geom_bar(width=0.8, stat = "identity", position="dodge")
p = subplot(g,d, nrows=2, shareX= T,which_layout = 1)
Hello,
I'm creating an object that when I click 2 objects Z, shows me above 2 distinct graphs, while below I would like to see the subtraction of the two.
Right now I can only see them distinct.
Can you help me to make a single chart but with only one bar given by the difference between the two data?
Thank you
I am designing a bar plot with ggplot2 package.
The only problem is that I cannot get rid of the space between the bars and the x-axis.
I know that this formula should resolve the problem:
scale_y_continuous(expand = c(0, 0)) function
But it seems that the element for the error bar is overwriting it and gives always this space.
here my code:
p<-ggplot(data=tableaumergectrlmut, aes(x=ID, y=meanNSAFbait, fill=Condition)) +
geom_bar(stat="identity", position=position_dodge())+
scale_y_continuous(expand = c(0,0))+
geom_errorbar(aes(ymin=meanNSAFbait-SDNSAFbait,
ymax=meanNSAFbait+SDNSAFbait, width=0.25), position=position_dodge(.9))
Using some example data to generate a plot that (I think) shows the problem you're having.
library(ggplot2)
df <- data.frame(val = c(10, 20, 100, 5), name = LETTERS[1:4])
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity")
There is a gap from the zero point on the y axis (bottom of the bars) and where the x axis labels are.
You can remove this using scale_y_discrete or scale_y_continuous, depending on the nature of your data, and setting expand to c(0,0):
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0))
This gives the plot:
Note I've also removed the gap along the y axis, simply remove the scale_x_discrete line to add this gap back in.
Since error bars are an issue, here are a few examples:
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymin = val - 10,
ymax = val + 10))
You can use scale to remove the padding down to the error bar:
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymin = val - 10,
ymax = val + 10)) +
scale_y_continuous(expand = c(0,0))
Or you can use coord_cartesian to give a hard cutoff:
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymin = val - 10,
ymax = val + 10)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(ylim = c(0, max(df$val) + 10))
Consider this sample data.
df <- data.frame(
x = factor(c(1, 1, 2, 2)),
y = c(.1, .3, .2, .1),
grp = c("a", "b", "a", "b")
)
Now I create the graph using ggplot, and annotate it using geom_text()
ggplot(data = df, aes(x, y, fill = grp, label = y)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(limits=c(0,1)) +
geom_text(position = position_dodge(0.9))
How do I specify that all the text values align perfectly horizontal at the top of the graph window?
You can specify the aes(y=...) in geom_text. So, for the numbers at the top of the graph window you'll have
ggplot(data = df, aes(x, y, fill = grp, label = y)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(y=Inf), position = position_dodge(0.9))
And you may want to chuck in a + ylim(0, 4) to expand the plot area.
To match the edited question:
ggplot(data = df, aes(x, y, fill = grp, label = y)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(limits=c(0,1)) +
geom_text(aes(y=0.9), position = position_dodge(0.9)) ## can specify any y=.. value