Changing Y-axis breaks in ggplot2 - r

I have this code to give the graph shown below:
d=ggplot(df, aes(x=Year, y=NAO_Index, width=.8)) +
+ geom_bar(stat="identity", aes(fill=NAO_Index>0), position='identity', col = 'transparent') +
+ theme_bw() + scale_fill_manual(values=c("royalblue", "firebrick3"), name="NAO Oscillation", labels=c("Negative", "Positive"), guide=guide_legend(reverse=TRUE)) +
theme(legend.position=c(0.06, 0.92)) +
+ theme(axis.title.x=element_text(vjust=-0.2)) +
+ geom_line(data=dfmoveav, aes(x=Year ,y=moveav)) +
+ ylab("NAO Index") +
+ ggtitle("NAO Index between 1860 and 2050") +
+ scale_x_continuous(breaks=c(seq(1860,2050,10))) +
+ scale_y_continuous(breaks=c(seq(-3.5,3.5,0.5)))
I am only really concerned with the last line. In the graph the y-axis only goes from -3 to 2.5. How do I get it from -3.5 to 3.5 so it is even?
I'm sure I am making a simple error but cannot figure it out!
Many thanks in advance.

You are almost there. Try to set limits.
d=ggplot(df, aes(x=Year, y=NAO_Index, width=.8)) +
+ geom_bar(stat="identity", aes(fill=NAO_Index>0), position='identity', col = 'transparent') +
+ theme_bw() + scale_fill_manual(values=c("royalblue", "firebrick3"), name="NAO Oscillation", labels=c("Negative", "Positive"), guide=guide_legend(reverse=TRUE)) +
theme(legend.position=c(0.06, 0.92)) +
+ theme(axis.title.x=element_text(vjust=-0.2)) +
+ geom_line(data=dfmoveav, aes(x=Year ,y=moveav)) +
+ ylab("NAO Index") +
+ ggtitle("NAO Index between 1860 and 2050") +
+ scale_x_continuous(breaks=c(seq(1860,2050,10))) +
+ scale_y_continuous(breaks=c(seq(-3.5,3.5,0.5)), limits = c(-3.5, 3.5))
More about it here
To map line into the legend you should map the variable to aesthetic. But it's not trivial and you'll find references to avoid this approach.
df <- data.frame(year=factor(seq(1:10)),
nao = rnorm(10, 0, 2),
mov = rnorm(10, 0,3))
df2 <- data.frame(year=factor(seq(1:10)),
mov = df$nao+rnorm(10, 0, 0.1),
g = .1)
ggplot() +
geom_bar(data = df, aes(x=year, y=nao, fill=nao > 0), width=.8,
stat="identity", position ='identity', col = 'transparent') +
geom_line(data = df2, aes(x = year, y = mov, group = g, size = g)) +
scale_fill_manual(values=c("royalblue", "firebrick3"),
name="NAO Oscillation",
labels=c("Negative", "Positive"),
guide=guide_legend(reverse=TRUE)) +
scale_size('Trend', range = 1, labels = 'Moving\naverage') +
ggtitle("NAO Index between 1860 and 2050") +
scale_y_continuous(breaks=c(seq(-5,5,0.5)), limits = c(-5, 5)) +
ylab("NAO Index") +
theme(legend.position = c(0.07, 0.80),
axis.title.x = element_text(vjust= -0.2),
legend.background = element_blank())
This may not be the best approach to map variables to aesthetics.

Related

How change color after certain intervals in ggplot2?

I have following data:
dput(data)
I used following code to generate the plot:
p <- ggplot(data, aes(Zscore, ID))
p + geom_point(aes(colour=pval, size=Hit)) +
scale_color_gradientn(colours=rainbow(4), limits=c(0, 1)) +
geom_vline(xintercept=0, size=0.2, colour="blue", linetype="dotted") +
theme(panel.background=element_rect(fill="gray95", colour="gray95"),
panel.grid.major=element_line(size=0.1,linetype='solid', colour="gray90"),
panel.grid.minor=element_line(size=0.1,linetype='solid', colour="gray90"),
axis.title.y=element_blank()) +
expand_limits(x=c(-2,3)) +
scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3)) +
scale_y_discrete(limits=rev(data$ID))
in the output, I want to change the color (to green) of positive Zscore values. Also to increase the hit to 0,10,20,25,50....
The plot below is maybe not what is wanted.
To have the color change to green when the Z-score is positive means it should be mapped to Zscore, not to pval;
Also, the aesthetic size for lines was deprecated in ggplot2 3.4.0, I am using linewidth instead.
library(ggplot2)
p <- ggplot(data, aes(Zscore, ID))
p + geom_point(aes(colour = Zscore > 0, size = Hit)) +
scale_color_manual(
name = 'Z-score',
labels = c("Negative", "Positive"),
values = c(`FALSE` = 'red', `TRUE` = 'green')
) +
scale_size_continuous(breaks = c(0,10,20,25,50, 75, 100)) +
geom_vline(xintercept=0, linewidth=0.2, colour="blue", linetype="dotted") +
theme(panel.background=element_rect(fill="gray95", colour="gray95"),
panel.grid.major=element_line(linewidth=0.1,linetype='solid', colour="gray90"),
panel.grid.minor=element_line(linewidth=0.1,linetype='solid', colour="gray90"),
axis.title.y=element_blank()) +
expand_limits(x=c(-2,3)) +
# scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3, 4, 5, 6)) +
scale_x_continuous(name = 'Z-score', breaks = pretty(data$Zscore)) +
scale_y_discrete(limits=rev(data$ID))
Created on 2022-11-18 with reprex v2.0.2

How can I add a legend to my ggplot? Im using geom_line and geom_ribbon

Good afternoon,
I use the following code to generate a plot:
ggplot() +
geom_line(data = disDataHeadItems, aes(x=disDataHeadItems$x,
y=disDataHeadItems$Freq)) +
geom_line(data = disDataLongTail, aes(x=disDataLongTail$x,
y=disDataLongTail$Freq)) +
xlab("Item id") +
ylab("# of occurrences") +
scale_x_continuous(breaks=seq(5, 75, 10), expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
geom_ribbon(aes(ymin=0, ymax=disDataHeadItems$Freq,
x=disDataHeadItems$x), fill="#CC6666") +
geom_ribbon(aes(ymin=0, ymax=disDataLongTail$Freq,
x=disDataLongTail$x), fill="#66CC99") +
geom_vline(xintercept=19, linetype="dotted")
This creates the following plot, which im very happy with :)
Now I want to add a legend which just indicated that the red part is the top-head items and the green part is the long tail of the data. When I look it up I only find answers that indicate how to alter the legend. But mine just does not show up. Any help is welcome :)
Putting the comments together and adding some random dataframe results in this:
library(ggplot2)
disDataHeadItems <- data.frame(
x = runif(100,1,100),
Freq = runif(100,5,10)
)
disDataLongTail <- data.frame(
x = runif(100,1,100),
Freq = runif(100,5,10)
)
ggplot() +
geom_line(data = disDataHeadItems, aes(x=x,
y=Freq)) +
geom_line(data = disDataLongTail, aes(x=x,
y=Freq)) +
xlab("Item id") +
ylab("# of occurrences") +
scale_x_continuous(breaks=seq(5, 75, 10), expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
geom_ribbon(aes(ymin=0, ymax=disDataHeadItems$Freq,
x=disDataHeadItems$x, fill="#CC6666")) +
geom_ribbon(aes(ymin=0, ymax=disDataLongTail$Freq,
x=disDataLongTail$x, fill="#66CC99")) +
geom_vline(xintercept=19, linetype="dotted")

R - Name of each bar in ggplot2

I would like to have the name of each bar under each bar (in my case the names are "Round" and they happen to be 1, 2, ... 12)
Here is my current code:
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") + ylim(c(0,40)) +
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5)
Here is the current output:
set your Round to be a factor
ggplot(data=draft1, aes(x = factor(Round), y = mean.age)) +
Or use scale_x_continuous()
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
... +
scale_x_continuous(breaks=(seq(1:12)))
Just add a discrete scale to x:
library(ggplot2)
draft1 = data.frame(Round = seq(1, 12), mean.age = sample(29:32))
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") + ylim(c(0,40)) +
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5) +
scale_x_discrete()

How can I add mean labels to a bar chart?

I would like to add the mean of each condition at the base of my bar chart in R. The final product looks something like this in excel (note the means are displayed at the base of each bar):
My current code is as follows:
pmrtBar <- ggplot(reslagdfClean,aes(x=Condition, y=PMMissProp*100)) + stat_summary(fun.y = mean, geom = "bar", fill = cbPalette) +
theme(axis.title=element_text(size=12)) +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=.1, size = .25) +
coord_cartesian(ylim=c(0,50)) +
labs(x = "Condition", y="Mean Mean Miss Proportion (%)") +
apatheme
pmrtBar
I am new the R environment. Any feedback on the code above is also appreciated.
It's always good to add a reproducible example to your question.
Converting my comment to an answer with the use of some example data:
# example data
dat <- data.frame(id = c("ACT","Blank","None"),
mn = c(0.3833,0.38,0.4033),
se = c(0.1,0.15,0.12))
# creating the plot
ggplot(dat, aes(x=id, y=mn, fill=id)) +
geom_bar(stat="identity", width=0.7) +
geom_errorbar(aes(ymax = mn + se, ymin = mn - se), width=0.25) +
geom_text(aes(y = 0.2, label = paste(mn*100, "%"))) +
labs(x = "\nCondition", y = "Proportion (%)\n") +
scale_y_continuous(breaks = seq(0.15, 0.55, 0.05), labels = scales::percent) +
coord_cartesian(ylim = c(0.15,0.55)) +
theme_minimal(base_size = 14) +
theme(panel.grid.major.y = element_line(linetype = 2, color = "grey80", size = 1))
which results in:

to show mean value in ggplot box plot

I need to be able to show the mean value in ggplot box plot. Below works for a point but I need the white dashed lines? Any body help?
x
Team Value
A 10
B 5
C 29
D 35
ggplot(aes(x = Team , y = Value), data = x)
+ geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9))
+ stat_summary(fun.y=mean, colour="red", geom="point")
Here's my way of adding mean to boxplots:
ggplot(RQA, aes(x = Type, y = engagementPercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "text", label="----", size= 10, color= "white") +
ggtitle("Participation distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
ggplot(df, aes(x = Type, y = scorepercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "point", shape= 23, size= 3, fill= "white") +
ggtitle("score distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
I would caution against using text to this and do geom_line instead as text is offset slightly and gives the wrong portrayal of the mean.
Hey user1471980, I think people are more inclined to help if you have a unique user name but then again you have a lot of points :)
this is a hack but does this help:
Value<-c(1,2,3,4,5,6)
Team<-c("a","a","a","b","b","b")
x<-data.frame(Team,Value) #note means for a=2, mean for b=5
ggplot(aes(x = Team , y = Value), data = x) + geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9)) +
annotate(geom="text", x=1, y=2, label="----", colour="white", size=7, fontface="bold", angle=0) +
annotate(geom="text", x=2, y=5, label="----", colour="white", size=7, fontface="bold", angle=0)

Resources