Adding lines to a qplot that don't cover whole axes - r

I want to add partial lines (dont expand whole plot) to a scatterplot. I have two separate plots working for this, but can't figure out how to join them together:
First plot -
p = qplot(x, y, data=data) + theme_bw() + theme(aspect.ratio=1)
lines = data.frame(x = c(-2,-2,5), y = c(0,2,2))
Second plot -
lines = data.frame(x = c(-2,-2,5), y = c(0,2,2))
base = ggplot(lines, aes(x, y))
base + geom_path(size = 1)
How to overlay them? Thanks.

Can you try:
p = qplot(x, y, data=data) + theme_bw() + theme(aspect.ratio=1)
lines = data.frame(x = c(-2,-2,5), y = c(0,2,2))
p+geom_line(data = lines, aes(x=x, y=y))
Taking an example dataset mydf:
Dept <- c("Res","Bankings","Customer","Legal","Collection","Business")
YScore <- c(1,2,3,1,2,3)
XScore <- c(2,1,3,1,2,4)
mydf<-data.frame(Dept,YScore,XScore)
p = qplot(XScore, YScore, data=mydf) + theme_bw() + theme(aspect.ratio=1)
lines = data.frame(x = c(-2,-2,5), y = c(0,2,2))
p+geom_line(data = lines, aes(x=x, y=y))

Related

Pass changed geom from object to other ggplot

I first make a plot
df <- data.frame(x = c(1:40, rep(1:20, 3), 15:40))
p <- ggplot(df, aes(x=x, y = x)) +
stat_density2d(aes(fill='red',alpha=..level..),geom='polygon', show.legend = F)
Then I want to change the geom_density values and use these in another plot.
# build plot
q <- ggplot_build(p)
# Change density
dens <- q$data[[1]]
dens$y <- dens$y - dens$x
Build the other plot using the changed densities, something like this:
# Built another plot
ggplot(df, aes(x=x, y =1)) +
geom_point(alpha = 0.3) +
geom_density2d(dens)
This does not work however is there a way of doing this?
EDIT: doing it when there are multiple groups:
df <- data.frame(x = c(1:40, rep(1:20, 3), 15:40), group = c(rep('A',40), rep('B',60), rep('C',26)))
p <- ggplot(df, aes(x=x, y = x)) +
stat_density2d(aes(fill=group,alpha=..level..),geom='polygon', show.legend = F)
q <- ggplot_build(p)
dens <- q$data[[1]]
dens$y <- dens$y - dens$x
ggplot(df, aes(x=x, y =1)) +
geom_point(aes(col = group), alpha = 0.3) +
geom_polygon(data = dens, aes(x, y, fill = fill, group = piece, alpha = alpha)) +
scale_alpha_identity() +
guides(fill = F, alpha = F)
Results when applied to my own dataset
Although this is exactly what I'm looking for the fill colors seem not to correspond to the initial colors (linked to A, B and C):
Like this? It is possible to plot a transformation of the shapes plotted by geom_density. But that's not quite the same as manipulating the underlying density...
ggplot(df, aes(x=x, y =1)) +
geom_point(alpha = 0.3) +
geom_polygon(data = dens, aes(x, y, fill = fill, group = piece, alpha = alpha)) +
scale_alpha_identity() +
guides(fill = F, alpha = F)
Edit - OP now has multiple groups. We can plot those with the code below, which produces an artistic plot of questionably utility. It does what you propose, but I would suggest it would be more fruitful to transform the underlying data and summarize that, if you are looking for representative output.
ggplot(df, aes(x=x, y =1)) +
geom_point(aes(col = group), alpha = 0.3) +
geom_polygon(data = dens, aes(x, y, fill = group, group = piece, alpha = alpha)) +
scale_alpha_identity() +
guides(fill = F, alpha = F) +
theme_minimal()

overlaying plots from different dataframes in ggplot without messing with legend

I want to overlay two plots: one is a simple point plot where a variable is used to control the dot size; and another is a simple curve.
Here is a dummy example for the first plot;
library(ggplot2)
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla")
Now lets overlay a curve to this plot with data from another dataframe:
df2 = data.frame(x=x, y=x^2-x+2)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
It produces the error:
Error in FUN(X[[i]], ...) : object 'v' not found
The value in v is not used to draw the intended curse, but anyway, I added a dummy v to df2.
df2 = data.frame(x=x, y=x^2-x+2, v=replicate(length(x),0)) # add a dummy v
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
An the result has a messed legend:
What is the right way to achieve the desired plot?
You can put the size aes in the geom_point() call to make it so that you don't need the dummy v in df2.
Not sure exactly what you want regarding the legend. If you replace the above, then the blue portion goes away. If you want to have a legend for the line color, then you have to place color inside the geom_line aes call.
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
df2 = data.frame(x=x, y=x^2-x+2)
ggplot(df, aes(x, y)) +
geom_point(aes(size = v)) +
theme_classic() +
scale_size("blabla") +
geom_line(data=df2, aes(x, y, color = "blue")) +
scale_color_manual(values = "blue", labels = "nanana", name = "other")

add y=0 line in some plots facet_grid ggplot2

I have a big plot, using facet_grid().
I want to add a vertical line to indicate y=0, but only in some of the plot.
Reproducible example -
df <- data.frame(x = 1:100, y = rnorm(100,sd=0.5), type = rep(c('A','B'), 50))
ggplot(df) + facet_grid(type~.) +
geom_point(data = df[df$type == 'A',], mapping = aes(x=x, y=y)) +
geom_rect(data = df[df$type == 'B',], mapping=aes(xmin=x,ymin=0,xmax=(x+2),ymax=y)) +
theme(panel.background=element_rect(fill="white"))
I want the line only in the top ptot for example.
Just create another data object for an hline geom and make sure to include the relevant faceted variable.
df <- data.frame(x = 1:100, y = rnorm(100,sd=0.5), type = rep(c('A','B'), 50))
ggplot(df) + facet_grid(type~.) +
geom_point(data = df[df$type == 'A',], mapping = aes(x=x, y=y)) +
geom_rect(data = df[df$type == 'B',], mapping=aes(xmin=x,ymin=0,xmax=(x+2),ymax=y)) +
geom_hline(data = data.frame(type="A", y=0), mapping=aes(yintercept=y)) +
theme(panel.background=element_rect(fill="white"))

qplot (ggplot2): plot of more functions with the same color

I'm plotting 11 curves and the program bellow works well. BUT I'm not able two change the wild colors to plot 11 black curves:
library(ggplot2)
#library(latex2exp)
library(reshape)
fn <- "img/plot.eps"
fct1 <- function(x0 ){
return(1/sin(x0)+1/tan(x0))
}
fct2 <- function(beta, t ){
return(2*atan(exp(t)/beta))
}
t<-seq(from=0,to=10,by=0.01)
s1<-cbind(t, fct2(fct1(-pi+0.0001),t),
fct2(fct1(-1.5),t),
fct2(fct1(-0.5),t),
fct2(fct1(-0.05),t),
fct2(fct1(-0.01),t),
fct2(fct1(0),t),
fct2(fct1(0.01),t),
fct2(fct1(0.05),t),
fct2(fct1(0.5),t),
fct2(fct1(1.5),t),
fct2(fct1(pi),t))
colnames(s1)<-c("time","y1","y2","y3","y4","y5","y6","y7","y8","y9","y10","y11")
s2 <- melt(as.data.frame(s1), id = "time")
q <- ggplot(s2, aes(x = time, y = value, color = variable))
q <- q + geom_line() + ylab("y") + xlab("t")+ ylab("x(t)")+
theme_bw(base_size = 7) + guides(colour = FALSE)
ggsave(file = fn, width = 2, height = 1)
q
EDIT Now the code should be reproducible
You need to map the variable to the grouping, and it will produce black lines by default.
q <- ggplot() +
geom_line(data = s2, aes(x = time, y = value,
group = variable)) +
xlab("t")+ ylab("x(t)") +
theme_bw(base_size = 7) + guides(colour = FALSE)
q
To be perfectly clear, it is possible to map the color to the variable, which can produce black lines, but not without changing the legend. Here is how you would amend the colors after the fact, if you wanted to, having already mapped the color to the variable.
q <- ggplot() +
geom_line(data = s2, aes(x = time, y = value,
color = variable)) +
xlab("t")+ ylab("x(t)") +
theme_bw(base_size = 7) + guides(colour = FALSE) +
scale_color_manual(values = rep("black",11))
q

ggplot2: Stat_function misbehaviour with log scales

I am trying to plot a point histogram (a histogram that shows the values with a point instead of bars) that is log-scaled. The result should look like this:
MWE:
Lets simulate some Data:
set.seed(123)
d <- data.frame(x = rnorm(1000))
To get the point histogram I need to calculate the histogram data (hdata) first
hdata <- hist(d$x, plot = FALSE)
tmp <- data.frame(mids = hdata$mids,
density = hdata$density,
counts = hdata$counts)
which we can plot like this
p <- ggplot(tmp, aes(x = mids, y = density)) + geom_point() +
stat_function(fun = dnorm, col = "red")
p
to get this graph:
In theory we should be able to apply the log scales (and set the y-limits to be above 0) and we should have a similar picture to the target graph.
However, if I apply it I get the following graph:
p + scale_y_log10(limits = c(0.001, 10))
The stat_function clearly shows non-scaled values instead of producing a figure closer to the solid line in the first picture.
Any ideas?
Bonus
Are there any ways to graph the histogram with dots without using the hist(..., plot = FALSE) function?
EDIT Workaround
One possible solution is to calculate the dnorm-data outside of ggplot and then insert it as a line. For example
tmp2 <- data.frame(mids = seq(from = min(tmp$mids), to = max(tmp$mids),
by = (max(tmp$mids) - min(tmp$mids))/10000))
tmp2$dnorm <- dnorm(tmp2$mids)
# Plot it
ggplot() +
geom_point(data = tmp, aes(x = mids, y = density)) +
geom_line(data = tmp2, aes(x = mids, y = dnorm), col = "red") +
scale_y_log10()
This returns a graph like the following. This is basically the graph, but it doesn't resolve the stat_function issue.
library(ggplot2)
set.seed(123)
d <- data.frame(x = rnorm(1000))
ggplot(d, aes(x)) +
stat_bin(geom = "point",
aes(y = ..density..),
#same breaks as function hist's default:
breaks = pretty(range(d$x), n = nclass.Sturges(d$x), min.n = 1),
position = "identity") +
stat_function(fun = dnorm, col = "red") +
scale_y_log10(limits = c(0.001, 10))
Another possible solution that I found while revisiting this issue is to apply the log10 to the stat_function-call.
library(ggplot2)
set.seed(123)
d <- data.frame(x = rnorm(1000))
hdata <- hist(d$x, plot = FALSE)
tmp <- data.frame(mids = hdata$mids,
density = hdata$density,
counts = hdata$counts)
ggplot(tmp, aes(x = mids, y = density)) + geom_point() +
stat_function(fun = function(x) log10(dnorm(x)), col = "red") +
scale_y_log10()
Created on 2018-07-25 by the reprex package (v0.2.0).

Resources