i'm new at R programming , how can i add a second y axis after plotting histogram so to plot geom_line
here's the code to plot histogram and geom_line, how can i solve my problem
I want to add a second axe y that can take into account the probability
library(ggplot2)
ggplot(x2, aes(x=X2, fill=as.factor(X1))) +
geom_histogram(alpha = 0.5,bins=100, position="dodge") +
geom_line(x2,mapping = aes(x = X2, y = probability))+
coord_cartesian(ylim = c(0, 150),xlim=c(0,20000000))+
xlab("Intensity")
Thank you
Related
I want to plot a segmented bar plot in ggplot2. Here is part of my dataframe, I want to plot the proportion of output(0 and 1) for each x1(0 and 1). But when I use the following code, what I plot is just black bars without any segmentation. What's the problem in here?
fig = ggplot(data=df, mapping=aes(x=x1, fill=output)) + geom_bar(stat="count", width=0.5, position='fill')
The output plot is here
You need factor variables for your task:
library(ggplot2)
df <- data.frame(x1=sample(0:1,100,replace = T),output=sample(0:1,100,replace = T))
ggplot(data = df, aes(x = as.factor(x1), fill = as.factor(output))) +
geom_histogram(stat = "count")+
labs(x="x11")
which give me:
I have an example data, which does not have x- and y-axis information. I would like to make a bubble plot using R package ggplot2, and arrange the bubbles in a circled manner.
data <- data.frame(group = paste("Group", letters[1:11]),
value = sample(seq(1,100),11))
Thanks a lot.
You can just put a dummy value for y and make group your x values in aes.
ggplot(data, aes(x = group, y = 0, size = value)) +
coord_polar() +
geom_point()
I have the following figure in R, generated using the ggplot2 package which resulted in the following:
The code to obtain this plot is:
df <- data.frame(value_x = c(10,20,30,40), value_y = c(89.3, 89.4, 89.60, 90.1))
myplot <- ggplot(data = df, aes(x = value_x, y = value_y)) +
geom_point() +
geom_line()
myplot
Now I want to fill the area under this curve, but still keep the y-axis scale.
When I add geom_area(alpha = 0.40) to the code, the plot becomes the following:
As you can see, the area runs from 0 to the curve, which rescales the y-axis. How can I inhibit this from happening?
I suggest the use of geom_ribbon which understands ymin and ymax aesthetics. Unlike geom_area which gives a continuous bar plot that starts at 0.
myplot +
geom_ribbon(aes(ymin = min(value_y), ymax = value_y))
I would like to plot a density plot using ggplot2, and make one section of the x-axis line thicker (or colored differently).
For example:
interval <- c(x1,x2)
x <- ggplot(df, aes(x=value)) + geom_density()
Is there any way to selectively make the x-axis segment corresponding to (x1,x2) thicker or colored differently? Thanks.
You can use annotate to add a line segment. Setting the y coordinates to -Inf will place it on the x axis. Since your example isn't reproducible, I've demonstrated on the mtcars data:
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
annotate(
geom = "segment",
x = 3, xend = 4,
y = -Inf, yend = -Inf,
color = "blue",
size = 5
)
I'm struggling to overlap rotated density plot onto the original scatterplot. Here are 2 plots I have:
require(ggplot2); set.seed(1);
df1 <- data.frame(ID=paste0('ID',1:1000), value=rnorm(1000,500,100))
p1 <- ggplot(data = df1, aes(x=reorder(ID, value), y=value)) +
geom_point(size=2, alpha = 0.7)+
coord_trans(y="log10")
p2 <- ggplot(data = df1, aes(x=value)) +
coord_trans(x="log10") +
geom_density() +
coord_flip()
p1
p2
First, there's a little problem with the density plot that its vertical axis is not log10-transformed. But main issue is that I can't find how to draw it on the previous plot keeping correct coordinates.
Because you are using coord_flip on your second plot you are effectively trying to plot two different values onto the same x axis (density and ID). There are plenty of posts discouraging this, here's one for example: How do I plot points with two different y-axis ranges on the same panel in the same X axis?.