How to change default aesthethics in ggplot2 for a group - r

I am trying to override the default settings in creating an error plot. For example, I have two different "Levels" in the following snippet:
plot<-ggplot(df, aes(x=variable, y=value, colour=Levels, group=Levels)) +
geom_errorbar(aes(ymin=value-se, ymax=value+se), colour="black", width=.1, position=pd) +
geom_line(position=pd) +
geom_point(position=pd, size=3, shape=21, fill="white"))
I want to plot a black and white display, with the geom_line displaying continuous and dashed lines for the two group members, and geom_point shading black for one and white for the other Level.
So far I cannot get lty=c("1","2") or fill=c("white", "black") to work.

plot<-ggplot(mdf, aes(x=variable, y=value, fill=Levels,
group=Levels,linetype=Levels))+
geom_errorbar(aes(ymin=value-se, ymax=value+se), colour="black", width=.1,
position=pd) +
geom_line(position=pd, colour="black")+scale_shape_manual(values=c("1","2"))+
geom_point(position=pd, size=3, shape=21, colour="black")+
scale_fill_manual(values=c("white","black")))

Related

How to draw lines at each panel divided by facet_wrap() in R?

Here is one data
variety<- c("CV1","CV1")
trt<- c("N0","N1")
yield<- c(100,150)
dataA<- data.frame(variety,yield,trt)
and I made a graph using facet_wrap() and drew lines.
ggplot(data=dataA, aes(x=variety, y=yield))+
geom_bar(stat="identity", position="dodge", width=0.7, size=1) +
coord_flip() +
facet_wrap(~ trt) +
geom_hline(yintercept=100, linetype = "dashed", color="Dark blue") +
geom_hline(yintercept=150, linetype = "dashed", color="Dark blue") +
windows(width=10, height=6)
when I drew two lines, it's duplicated at each panel. I'd like to draw a line at each each panel like below.
Could you let me know about that?
Always many thanks
Use aes to add yintercept in the aesthetics:
ggplot(data=dataA, aes(x=variety, y=yield))+
geom_bar(stat="identity", position="dodge", width=0.7, size=1) +
coord_flip() +
facet_wrap(~ trt) +
geom_hline(aes(yintercept = c(100, 150)), linetype = "dashed", color="Dark blue")

Stacked bar graph in R: re-ordering the data

I've produced a bar graph, however I want the data to be switched.
I want the blue area (the live cells) on the bottom and the dead cells on the top.
I tried reorder, reordering it by making it a factor but it all didn't work.
Who can help me?
Code I have now:
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) +
geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
Graph I have now:
library(forcats)
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=fct_shift(Type))) +
geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
Have you tried position_stack(reverse = TRUE)?
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) +
geom_bar(aes(fill = Type), stat="identity", position = position_stack(reverse = TRUE)), color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
`install.packages("forcats")
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill = forcats::fct_rev(Type))) +
geom_bar(stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey"))`
Forcats did the trick

ggplot scale alpha continuous fill color

I've tried to create an alpha plot but I couldn't find right way to do it. I tried different combination to figure out and I've almost there but I need a little help.
My question is how can I get rid off blue color in the plot.
My script is `
p <- ggplot(df, aes(x=x, y=y))
p + geom_hex(aes(alpha=..count..),bins=20)+
scale_x_log10("ratio following/followers",
labels = trans_format("log10", math_format(10^.x))) +
scale_y_log10("ratio messages received/sent",
labels = trans_format("log10", math_format(10^.x))) +
theme_bw() +
theme(panel.background = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor=element_blank(),
plot.background = element_blank())+
#guides(fill=FALSE)+
scale_alpha_continuous ("Counts",breaks=c(0,2000,4000,6000,8000,10000))+
geom_vline(xintercept =1, color="red", size=0.25, linetype=5)+
geom_hline(yintercept =1, color="red", size=0.25, linetype=5) +
annotate('text', x=500, y=0.01, size=3, label="4\ncommon\nusers") +
annotate('text', x=0.0001, y=0.01, size=3, label="3\nbroadcasters") +
annotate('text', x=0.0001, y=7000, size=3, label="1\ninfluentials") +
annotate('text', x=500, y=7000, size=3, label="2\nhidden\ninfluentials")
This script creates this plot
I can be able to get rid off blue legend with activating "guides(fill=FALSE)+" line in the script and it give this:
You can reach sample data from here
Thanks #Didzis Elferts for his answer. I couldn't be sure about the legend and plot breaks colors. As you can see these pictures 10K and 8K has the same color (Am I right!) so 10K should be darker, shouldn't be.
Function geom_hex() by default maps counts to the fill and so you get fill gradient (blue by default). If you just want map counts to alpha values then you must assign fill = outside the aes() of geom_hex() to some color (used grey45 as example). As fill = is set outside the aes() there won't be legend for fill values.
p + geom_hex(aes(alpha=..count..),bins=20, fill = "grey45")+ ...

ggplot: conflict between geom_text and ggplot(fill)

When I use geom_text on a ggplot, there is a conflict with the ggplot "fill" option.
Here is a clear example of the problem:
library(ggplot2)
a=ChickWeight
str(a)
xx=data.frame(level=levels(a$Chick),letter=1:50)
# a graph with the fill option alone
x11();ggplot(a, aes(x=Chick, y=weight,fill=Diet)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
xlab("Chick") +
ylab("Weight")
# a graph with the geom_text option alone
x11();ggplot(a, aes(x=Chick, y=weight)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=450,label = letter)) +
xlab("Chick") +
ylab("Weight")
# a graph with the two option
x11();ggplot(a, aes(x=Chick, y=weight,fill=Diet)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter)) +
xlab("Chick") +
ylab("Weight")
If you only want the fill to affect the boxplot, move the aes() into the boxplot. Any aes() aesthetics in the ggplot() call itself will be propagated to all layers
ggplot(a, aes(x=Chick, y=weight)) + geom_boxplot(aes(fill=Diet), notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter)) +
xlab("Chick") +
ylab("Weight")
you can also disable the fill= aesthetic in the text layer with fill=NULL
ggplot(a, aes(x=Chick, y=weight, fill=Diet)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter, fill=NULL)) +
xlab("Chick") +
ylab("Weight")

Multiple indices in ggplot label

I would like to add a label containing multiple indices to a bar plot:
data<-as.data.frame(c("A"))
colnames(data)<-"A"
data$B<-5
ggplot(data, aes(x=A, y=B)) +
geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) +
annotate("text", x=1, y=2.5, label="some text")
I need to replace "some text" with "a1 a2 a3" where the numbers are in subscript. I tried the following but get errors:
annotate("text", x=1, y=0.4, parse=T, label=paste("a[1]","a[2]","a[3]",sep=" "))
The following does that:
ggplot(data, aes(x=A, y=B)) + geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) + annotate("text", x=1, y=2.5, label="~a[1]~a[2]~a[3]", parse=TRUE)

Resources