Mapping mean values onto count diagram using ggplot2 in R - r

I have a data frame as follows:
variable=c("D","D","C","C","C","A","B","B","B","B")
value=c(80,100,70,68,65,45,33,31,36,32)
Count=as.integer(c(5,10,4,5,2,7,3,5,6,2))
mean=c(93.3,93.3,68.2,68.2,68.2,45,33.4,33.4,33.4,33.4)
df=data.frame(variable=variable,value=value,Count=Count,mean=mean)
I can make a nice plot (where the size of the square corresponds to the count of observations with that particular x-value and y-value), as shown below:
ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade")
However, I now want to superimpose a horizontal bar to each of the four topics, indicating the mean percentage grade. Those values are stored in df$mean. I cannot figure out how to accomplish this. I have tried using the geom_line() function with the horizontal line option... but this seems to plot vertical lines!
ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade") + geom_line(stat = "hline", yintercept = df$mean)
Thank you...

You can do this with geom_segment:
ggplot(df, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
geom_segment(aes(x=variable, y=mean-.1,
xend=variable, yend=mean+.1),
color="red", size=I(40))

Related

Adjust the position of each text to the top center of its grouped vertical bar plot columns in ggplot2

By the code below, I generate a grouped bar plot below:
df <- data.frame(Person = c("Mr.A","Mr.B"), Cats = c(3,4), Dogs = c(1,2))
data.m <- melt(df, id.vars='Person')
ggplot(data.m, aes(Person, value)) +
geom_bar(aes(fill = variable), width = 0.4, position = position_dodge(width=0.5), stat="identity") +
geom_text(
aes(x = Person, y = value, label=value),
position = position_dodge(width=1),
# position = 'dodge',
# hjust=-1,
vjust=-0.25) +
theme(legend.position="top",
legend.title = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
Now my question is how could we adjust the text in the middle of each bar column (as the arrows shown in the figure above), I tried by paramters: position=position_dodge(width=1), position='dodge', hjust=-1 and hjust=1, it seems not working. Someone could help? Thanks.
Reference:
Aligning geom_text in grouped dodged barplot
You need to specify group=variable in geom_text.
ggplot(data.m, aes(Person, value)) +
geom_bar(aes(fill = variable), width = 0.4, position = position_dodge(width=0.5), stat="identity") +
geom_text(
aes(x = Person, y = value, label=value, group = variable),
position = position_dodge(width=0.5),
# position = 'dodge',
# hjust=-1,
vjust=-0.25) +
theme(legend.position="top",
legend.title = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())

Scale the distance between ticks on x-axis in gglot r

I have a graph like this. I am interested in the minor change between the range (-3.5, 0.5), however, the occupied only a small portion of the x-axis, so that it's hard to interpret.
I tried to use transform to log scale for better visualization, however, it apparently not works for negative values.
So is there any method to expand this region to make the graph look nicer?
Code:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18))
Something like this
library(ggforce)
library(ggolot2)
ggplot(mtcars, aes(x=mpg, y=disp, group=1)) +
geom_line() + facet_zoom(xlim = c(15, 20))
You may try adding the xlim = c(minor value, major value) option of ggplot, and use the range which works better for you
Something like that:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18)) +
xlim = c(-4, 1)

ggplot2: increasing space between categorical axis ticks with geom_point

I have a geom_point plot that with a large number of categorical variables, and a size parameter mapped to a continuous variable. When I make the plot, the categorical variables are too close together, and the large points from within each overlap. Is there any way to give a little breathing room to the axis so that this doesn't happen? I'm aware that an alternative solution is simply to use scale_size_area(max_size = 3) to narrow the range of point sizes, but I'd prefer not to do this as it makes it too difficult to tell them apart.
Here's the code:
plot <- ggplot(allcazfull, aes(x = Family, y = ifelse(Percentage==0,NA, Percentage), fill = Treatment, size = ifelse(Number == 0, NA,Number))) +
facet_wrap(~ Pathogen, scales = "free_x") +
geom_point(shape = 21) +
scale_fill_manual(values = alpha(c("#98fb98","#f77e17","#0d5a0d","#8d0707"),.6)) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
aspect.ratio = 4/1,
strip.background = element_rect(fill="white", linetype = "blank"),
strip.text = element_blank()) +
scale_x_discrete(limits = rev(levels(allcazfull$Family))) +
xlab("") +
ylab("") +
guides(fill = FALSE, size = FALSE) +
coord_flip()
plot
And here's the resulting figure:

Multiplot facet line plot in ggplot2 with two Y axes - adding points and changing line colours

I am preparing a multiplot for with two y axes for Inc and Ratio.
I distinguished each plot with different colours to represent three regions.
There are three items I am not successful at:
I do have now, two lines with the same colour in each plot. I would like to change one of them to be dashed (Ratio one).
I need to add SE bars to Inc line (from Inc column)
I would like to add geom_points() so there are also points at the nodes where lines are connecting, only for aesthetic reasons.
This is as far as I get:
df <- data.frame(c(2009,2009,2009,2009,2010,2010,2010,2010,2011,2011,2011,2011,
2012,2012,2012,2012,2013,2013,2013,2013),
c("N","S","W","W","N","S","W","W","N","S","W","W","N","S","W","W",
"N","S","W","W"),
c("Luo","Aka","Opo","Mya","Luo","Aka","Opo","Mya",
"Luo","Aka","Opo","Mya","Luo","Aka","Opo","Mya",
"Luo","Aka","Opo","Mya"),
runif(20,0,1),runif(20,0,1),
runif(20,0,0.1))
colnames(df) <- c("Year","Region","District","Inc","Ratio","Inc_SE")
# Order of drawing in facet
df$District<- factor(df$District,
levels = c("Opo",
"Mya",
"Luo",
"Aka"))
p <- ggplot(data=df, aes(x = Year))
p <- p + geom_line(aes(y = Inc))
p <- ggplot(df, aes(x = Year, y=df$Inc))
p <- p + geom_line(aes(y = Inc))
p <- ggplot(df, aes(x = Year))
p <- p + geom_line(aes(y = Inc, colour = Region))
p <- p + theme_bw()+
theme(plot.title = element_text(hjust = 1))+
theme(legend.position="none")+
theme(axis.title.x = element_text(face ="bold", colour="black", size=11),
axis.text.x = element_text(angle=90, vjust=0.5, size=7, family = "serif"),
axis.title.y = element_text(face = "bold", colour = "black", size=10))
# adding Ratio
p <- p + geom_line(aes(y = Ratio, colour = Region,linetype = "dashed")) # here dashed is not recognised by R
# now adding the secondary axis
p <- p + scale_y_continuous(sec.axis = sec_axis(~.*1, name = "Ratio"))
p <- p + scale_colour_manual(values = c("blue", "red","black"))
p <- p +
theme_bw()+
theme(plot.title = element_text(hjust = 1))+
theme(legend.position="none")+
theme(axis.title.x = element_text(face ="bold", colour="black", size=11),
axis.text.x = element_text(angle=90, vjust=0.5, size=9, family = "serif"),
axis.title.y = element_text(face = "bold", colour = "black", size=10))
# Breaking down to separate graphs
p_facet = p + facet_wrap(~ df$District,
ncol = 2)
p_facet
You can try a tidyverse. The trick is to transform the data from wide to long (here I used gather). then you can easily add points, lines and Inc_SE as ribbon.
library(tidyverse)
df %>%
gather(k,v, -Year, -Region, -District, -Inc_SE) %>%
ggplot(aes(Year, v, group = k, color=Region, linetype=k)) +
geom_ribbon(data=. %>% filter( k == "Inc"),
aes(ymin=v-Inc_SE, ymax=v+Inc_SE),
alpha=0.2,color=NA,
show.legend = F) +
geom_line() +
geom_point(show.legend = F)+
scale_y_continuous(sec.axis = sec_axis(~.*1, name = "Ratio"))+
facet_wrap(~ District) +
labs(y="Inc") +
theme_bw() +
theme(legend.position = "bottom")

In ggplot2 how can I scale the legend when using two graph types?

I'm using ggplot2 with both + geom_line() + geom_point(). I have the colors/shapes worked out, but I can't scale the legend appropriately. If I do nothing it's tiny, and if I enlarge it, the color blocks the shape.
For example:
You can see that the shapes and colors are both in the legend, but the shapes are being drawn over by the colors. I would like to have shapes of the appropriate color drawn in the legend, but can't figure out how to do it.
My plot is being drown as follows:
ggplot(data=melted, aes(x=gene, y=value, colour=variable, shape=variable, group = variable, stroke=3, reorder(gene, value)))
+ theme_solarized()
+ scale_colour_solarized("blue")
+ geom_line()
+ geom_point()
+ theme(axis.text.x = element_text(angle = 90, hjust = 1), plot.title = element_text(size=16, face="bold"), legend.title=element_blank(), legend.text=element_text(size=20))
+ ggtitle('Signiture Profiles')
+ labs(x="Gene", y=expression(paste("Expression"), title="Expression"))
+ scale_colour_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c("#ff420e","#89da59","#89da59","#89da59","#376467","#376467","#376467","#00293c","#00293c","#00293c"))
+ scale_shape_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c(0,1,2,3,1,2,3,1,2,3))
+ guides(colour = guide_legend(override.aes = list(size=12)))
Here is some example data as requested:Example Data
Thanks in advance for any help you can provide.
You could perhaps rethink how you are differentiating your variables.
You could do something like the following. Note the changes in the first line, where I have separated the component parts of variable rather than setting colours and shapes via your scale statements. (I haven't got your theme, so I left that out).
ggplot(data=melted, aes(x=gene,
y=value,
colour=gsub("_.*","",variable),
shape=gsub(".*_","",variable),
group = variable,
stroke=3,
reorder(gene, value))) +
geom_line() +
geom_point() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
plot.title = element_text(size=16, face="bold"),
legend.title=element_blank(),
legend.text=element_text(size=20)) +
ggtitle('Signiture Profiles') +
labs(x="Gene", y=expression(paste("Expression"), title="Expression")) +
guides(shape = guide_legend(override.aes = list(size=5)),
colour = guide_legend(override.aes = list(size=5)))

Resources