I am new to R, and I'm trying to use ggplot2 to plot some data as a scatterplot. I'm missing a day in my samples, and the trendline I made won't connect all of the data together. Below is the code I have and what the graph looks like.
ggplot(SiExptTEPa, aes(x=Timepoint..dpi., y=(TEPcells),group=(Treatment))) +
geom_point(size=5,aes(colour=Nutrient)) +
scale_color_manual(values=c('yellow','light blue')) +
geom_errorbar(aes(ymin=TEPcells-se, ymax=TEPcells+se), width=.1) +
facet_wrap(~Nutrient, scales="free") +
scale_y_continuous(labels = scientific) +
theme_classic() +
xlab("Time Post Infection (Days)") +
ylab("TEP by Total Cells") +
ylim(3e-08,2e-07) +
geom_line(aes(linetype=Treatment)) +
scale_linetype_manual(values=c("solid", "dashed"))
Incorrect graph
Please tell me how to connect the gap in the middle of both sides of the graph so that there is one continuous line?
Related
I draw a barchart in R:
ggplot(data, aes(x=rating, fill=rating)) +
geom_bar(stat="count") +
ggtitle("Rating in stories")+
coord_flip()+
xlab("rating")+
ylab("number of stories")+
theme(legend.position="none")
The result is here.
The bars represent the amount of times the specific value (M, T, K or K+) occurs in the rating variable.
How do I sort the bars decreasingly?
OK, I found what I was looking for. I needed to use fct_rev(fct_infreq()) on the variable.
ggplot(data, aes(forcats::fct_rev(fct_infreq(rating)), fill=rating)) +
geom_bar(stat = "count") +
ggtitle("Rating in stories")+
coord_flip()+
xlab("rating")+
ylab("number of stories")+
theme(legend.position="none")
I want to plot a negative binomial distribution and a Poisson distribution to fit my real data, but I don't know how to plot a legend, who can help me with that, thanks a lot. My code and picture is as follows:
ggplot() +
geom_density(aes(a),color="red",lwd=2) +
geom_density(aes(x=rpois(50,1.57)),color="purple",lwd=2) +
geom_smooth() +
geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57)),color="blue",lwd=2) +
geom_smooth() +
coord_cartesian(xlim=c(0,10)) + labs(x="count")
And my data was uploaded here:
https://www.jianguoyun.com/p/DSHXKgMQm5CLBhiKjCc.
The easiest way to add a legend is to map a variable to color. For example
ggplot() +
geom_density(aes(a, color="data"),lwd=2) +
geom_density(aes(x=rpois(50,1.57), color="poisson"),,lwd=2) +
geom_smooth() +
geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57),color="binomial"),lwd=2) +
geom_smooth() +
coord_cartesian(xlim=c(0,10)) + labs(x="count")
If I plot this
dodge <- position_dodge(.35)
ggplot(mediat, aes(x=t, y=Value, colour=factor(act),group=id )) +
geom_point(position=dodge) + geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt),
width=0, position=dodge) + theme_bw() + geom_smooth(method="lm",se=FALSE,
fullrange=TRUE)
I get this
As you can see the regression line is not plotted.
with +stat_smooth(method=lm, fullrange=TRUE, se = FALSE) the result is the same.
I've found that removing the "group=id" I can get the regression lines but
then
ggplot(mediat, aes(x=t, y=Value, colour=factor(act) ))+ geom_point(position=dodge) +
geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt), width=0, position=dodge) +
theme_bw() + geom_smooth(method="lm",se=FALSE, fullrange=TRUE)
As you can see, now it plot the lines but I loose the dodge function by groups.
How can I get both things at once?. I mean, regression lines by "id" on the first uncluttered plot?
Any other solution with base plot, lattice or any other common package would also be welcome.
Regards
I'm needing to plot two regressions on the same axes. For this I have 3 columns in my dataset (let's call them A, B & C). I want to plot B against A and then C against A, and have these as different colour regression lines, with the data points being the same colour as their corresponding lines.
To be more specific, to create individual plots I used the following code for the first one:
P1 <- ggplot(data=volboth, aes(x=control, y=vol30)) +
geom_point(alpha=1, size=4, color="maroon") +
ggtitle("Correlation Plot: Ground Survey (Control) vs 30m UAV Survey") +
labs(x = expression(paste("Volume - Control Data - ", m^{3})),
y = expression(paste("Volume - Aerial Data - ", m^{3}))) +
xlim(0, 5) +
ylim(0, 5) +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
And then the following for the second plot:
P2 <- ggplot(data=volboth, aes(x=vol10, y=control)) +
geom_point(alpha=1, size=4, color="maroon") +
ggtitle("Correlation Plot: Ground Survey (Control) vs 10m UAV Survey") +
labs(x = expression(paste("Volume - Aerial Data - ", m^{3})),
y = expression(paste("Volume - Control Data - ", m^{3}))) +
xlim(0, 5) +
ylim(0, 5) +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
Any ideas of how to combine both plots onto same axes, and to apply corresponding visual themes? I'm open to using standard R (not ggplot2) if that makes things easier.
require(ggplot2)
#first, some sample data
volboth <- data.frame(control=(0:100)/20,vol10=(50:150)/50,vol30=(120:20)/30)
#next, make a plot
P1 <- ggplot(data=volboth, aes(x=control, y=vol30)) +
geom_point(alpha=1, size=4, color="maroon") +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE) +
#Now add a second layer, with same x, but other y (and blue color for clarity)
geom_point(aes(y=vol10),alpha=1, size=4, color="blue") +
geom_smooth(aes(y=vol10),method=lm, se=FALSE, fullrange=TRUE) +
ggtitle("Correlation Plot: Ground Survey (Control) vs 30m UAV Survey") +
labs(x = expression(paste("Volume - Control Data - ", m^{3})),
y = expression(paste("Volume - Aerial Data - ", m^{3}))) +
xlim(0, 5) +
ylim(0, 5)
print(P1)
Which gives me this graph:
I have used geom_point here, but as you have worked out yourself if your points are closely together, geom_jitter might be a better alternative.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using ggplot2, can I insert a break in the axis?
I'm using the following ggplot2 code to generate a faced_grid barplots:
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") +
facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none")
Which gives the following plot (screenshot of the first facet):
As you can see the y-axis get stretched to quite a high value because of 1 outlier. What I'd like to do is create a more sensible scaling by having more ticks until 2e+05 and then just have 1 tick that goes directly towards 5e+05. This way the scaling would not be linear anymore but it would allow to show that there is a massive peak for 1 of the categories.
Is there anyway of doing this simple with ggplot2? Is there a R trick for doing this? If possible I'd not like to use things like ylim to just not show the top anymore.
You could use a transformation on the y-axis. Untested since you did not provide a reproducible example.
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + scale_y_log10()
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + scale_y_sqrt()
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + coord_trans(y = "log10")
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + coord_trans(y = "sqrt")