How I can modify the following code to have a plot between x and y (scatter plot) and the fitted values X0.025 and y=X0.975 as the curves (lines) on the plot. (please run plot(m6) to see the plot which I am looking for to make by ggplot)
library(quantregGrowth)
data(growthData)
m6<-gcrq(y~ps(x, lambda=seq(0,100,l=20)), tau=c(0.025,0.975), n.boot=10,
data=growthData)
plot(m6)
I tried to make this plot by ggplot and here is the code:
library(ggplot2)
library(plotly)
temp <- data.frame(m6$fitted)
growthData_b <- cbind(growthData, temp)
a <- ggplot(data=growthData_b, aes(x, y=X0.025)) + geom_line() +
geom_line(data=growthData_b, aes(x, y=X0.975), color = "red") + theme_bw()
Are you looking for this?
ggplot(data=growthData_b, aes(x, y=X0.025)) +
geom_line() +
geom_line(data=growthData_b, aes(x, y=X0.975), color = "red", linetype = 2) +
theme_bw() +
geom_point(aes(x=x, y=y), shape = 1)
Related
I have done a clustering dendrogram following a previous code I found online, but the x-axis of is not being shown in the graph. I would like to have the dissimilarity value shown in the x-axis, but I have not been successful.
females<-cervidae[cervidae$Sex=="female",]
dstf <- daisy(females[,9:14], metric = "euclidean", stand = FALSE)
hcaf <- hclust(dstf, method = "ave")
k <- 3
clustf <- cutree(hcaf,k=k) # k clusters
dendrf <- dendro_data(hcaf, type="rectangle") # convert for ggplot
clust.dff <- data.frame(label=rownames(females), cluster=factor(clustf),
females$Genus, females$Species)
dendrf[["labels"]] <- merge(dendrf[["labels"]],clust.dff, by="label")
rectf <- aggregate(x~cluster,label(dendrf),range)
rectf <- data.frame(rectf$cluster,rectf$x)
ymax <- mean(hcaf$height[length(hcaf$height)-((k-2):(k-1))])
fem=ggplot() +
geom_segment(data=segment(dendrf), aes(x=x, y=y, xend=xend, yend=yend)) +
geom_text(data=label(dendrf), aes(x, y, label= females.Genus, hjust=0,
color=females.Genus),
size=3) +
geom_rect(data=rectf, aes(xmin=X1-.3, xmax=X2+.3, ymin=0, ymax=ymax),
color="red", fill=NA)+
coord_flip() + scale_y_reverse(expand=c(0.2, 0)) +
theme_dendro() + scale_color_discrete(name="Genus") +
theme(legend.position="none")
Here is how my dendrogram looks:
Your code included theme_dendro(), which is described in its help file as:
Sets most of the ggplot options to blank, by returning blank theme
elements for the panel grid, panel background, axis title, axis text,
axis line and axis ticks.
You force the x-axis line / text / ticks to be visible in theme():
ggplot() +
geom_segment(data=segment(dendrf), aes(x=x, y=y, xend=xend, yend=yend)) +
geom_text(data=label(dendrf), aes(x, y, label= label, hjust=0,
color=cluster),
size=3) +
geom_rect(data=rectf, aes(xmin=X1-.3, xmax=X2+.3, ymin=0, ymax=ymax),
color="red", fill=NA)+
coord_flip() +
scale_y_reverse(expand=c(0.2, 0)) +
theme_dendro() +
scale_color_discrete(name="Cluster") +
theme(legend.position="none",
axis.text.x = element_text(), # show x-axis labels
axis.ticks.x = element_line(), # show x-axis tick marks
axis.line.x = element_line()) # show x-axis lines
(This demonstration uses a built-in dataset, since I'm not sure what's cervidae. Code used to create this is reproduced below:)
library(cluster); library(ggdendro); library(ggplot2)
hcaf <- hclust(dist(USArrests), "ave")
k <- 3
clustf <- cutree(hcaf,k=k) # k clusters
dendrf <- dendro_data(hcaf, type="rectangle") # convert for ggplot
clust.dff <- data.frame(label=rownames(USArrests),
cluster=factor(clustf))
dendrf[["labels"]] <- merge(dendrf[["labels"]],clust.dff, by="label")
rectf <- aggregate(x~cluster,label(dendrf),range)
rectf <- data.frame(rectf$cluster,rectf$x)
ymax <- mean(hcaf$height[length(hcaf$height)-((k-2):(k-1))])
I want to make a plot in ggplot2 like this
x <- 1:10
y <- rnorm(10) + x
df <- data.frame(x=x,y=y)
plot(x,y)
lines(x,y,type='c')
I like this type='c' style,R help tell me
"c" for empty points joined by lines
but how can I implement this type in ggplot2?
library(ggplot2)
ggplot(data=df,aes(x=x,y=y)) + geom_line() + geom_point(shape=21,size=3)
I mean, how to make a blank between the empty points and the lines? which linetype should I choose?
thank you very much
ggplot(data=df,aes(x=x,y=y)) +
geom_line() +
geom_point(shape=21, size=5, colour="white", fill="white") +
geom_point(shape=21,size=3) +
theme_bw()
Below we see a graph with two plots. I would like to have the series S1 with the same colour in each of the plots.
However, it seems that the colours are being attributed by alphabetic order.
The code I'm using is the following:
plot1<-ggplot(data=dfp)+
geom_point(aes(x=Save,y=Obsdata,colour="S1"))+
geom_point(aes(x=Save,y=BiasCorrected,colour="S2"))+
xlab("X")+ylab("Y")+
scale_color_discrete(name="")+
theme(legend.position="bottom")
plot2<-ggplot(data=dfp)+
geom_point(aes(x=Save,y=SModel, colour="R3"))+
geom_point(aes(x=Save,y=Obsdata,colour="S1"))+
xlab("X")+ylab("Y")+
scale_color_discrete(name="")+
theme(legend.position="bottom")
grid.arrange(plot1, plot2, ncol=2)
Any help would be appreciated.
You can specify a manual color palette like this:
df <- data.frame(x=runif(90), y=runif(90), col=gl(3,30,labels=LETTERS[1:3]), fac=gl(2,45))
library(ggplot2)
p1 <- ggplot(df[df$fac==1,], aes(x,y,color=col)) + geom_point()
p2 <- ggplot(df[df$fac==2,], aes(x,y,color=col)) + geom_point()
pal <- list(scale_color_manual(values = c("A"="red", "B"="blue", "C"="darkgreen")))
gridExtra::grid.arrange(p1 + pal, p2 + pal, ncol = 2)
Also note the facetted option ggplot(df, aes(x,y,color=col)) + geom_point() + facet_wrap(~fac).
I plot data with ggplot, and I wanted to see the smoothed lines using stat_smooth.
But now I would like to only plot the smoothed lines (somehow extract it), without the original ggplot.
Do you think it's possible?
Here is my code :
Graph <- ggplot(data=Forecasttemp, aes(x=Price.date, y=Price, colour=Group)) + geom_line() + scale_colour_hue(guide = "none")
Graph <- Graph + stat_smooth(se = FALSE, aes(fill = Group)) + scale_colour_hue(guide = "none")
If you want to plot only the smoothed lines without original sample points, you can simply omit geom_line(), thus resulting in:
Graph <- ggplot(data=Forecasttemp, aes(x=Price.date, y=Price, colour=Group)) +
stat_smooth(se = FALSE, aes(fill = Group)) +
scale_colour_hue(guide = "none")
Unfortunately I can not try this due to the lack of a reproducible example, but I make a try with an R base dataset and it worked:
library(ggplot2)
data(iris)
g1 <- ggplot(data=iris, aes(x=Sepal.Length, y=Petal.Length, colour=Species)) +
scale_colour_hue(guide = "none") + geom_smooth()
g1
I'm using ggplot to create the plot of the density of my data.But I want to get plot for the X value until 2.0.
How can I put this restiriction in my code?
Here is my code and the plot:
ggplot()+geom_density(aes(data=Ge,x=GeN[,1]),color='red')
ggplot(iris, aes(x = Petal.Length)) + geom_density()
ggplot(iris, aes(x = Petal.Length)) + geom_density() + xlim(0, 2)