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:
Related
I'm trying to plot a line graph (data points between 0 and 2.5, with interval of 0.5). I want to plot some bars in the same chart on the right-hand axis (between 0 and 60 with interval of 10). I am making some mistake in my code such that the bars get plotted in the left hand axis.
Here's some sample data and code:
Month <- c("J","F","M","A")
Line <- c(2.5,2,0.5,3.4)
Bar <- c(30,33,21,40)
df <- data.frame(Month,Line,Bar)
ggplot(df, aes(x=Month)) +
geom_line(aes(y = Line,group = 1)) +
geom_col(aes(y=Bar))+
scale_y_continuous("Line",
sec.axis = sec_axis(trans= ~. /50, name = "Bar"))
Here's the output
Thanks in advance.
Try this approach with scaling factor. It is better if you work with a scaling factor between your variables and then you use it for the second y-axis. I have made slight changes to your code:
library(tidyverse)
#Data
Month <- c("J","F","M","A")
Line <- c(2.5,2,0.5,3.4)
Bar <- c(30,33,21,40)
df <- data.frame(Month,Line,Bar)
#Scale factor
sfactor <- max(df$Line)/max(df$Bar)
#Plot
ggplot(df, aes(x=Month)) +
geom_line(aes(y = Line,group = 1)) +
geom_col(aes(y=Bar*sfactor))+
scale_y_continuous("Line",
sec.axis = sec_axis(trans= ~. /sfactor, name = "Bar"))
Output:
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'm facing this problem with ggplot2 and plotly packages with R.
I'm creating a stacked bar chart, and, first of all, my data:
# here my data
family <- c('one','one','one','one','one','one','one','one',
'two','two','two','two','two','two','two','two')
group <- c('alpha','beta','alpha','beta','alpha','beta',
'alpha','beta','alpha','beta','alpha','beta',
'alpha','beta','alpha','beta')
field <- c('a','a','b','b','c','c','d',
'd','a','a','b','b','c','c','d','d')
value <- sample(1:10, 16, replace =TRUE)
data <- data.frame(family, group, field, value)
remove(family,group,field,value)
Then, I created my stacked barchart with ggplot2:
# here the plot
library(ggplot2)
library(plotly)
p <- ggplot(data, aes(x = group, y = value, fill =reorder(field,value)))
p <- p + geom_bar(position = "fill",stat = "identity")
p <- p + ggtitle("A simple example")
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))+ guides(fill=FALSE)
p <- p + facet_grid(family~.)
Lastly, I put it as a plotly output:
ggplotly(p)
remove(data)
The result is good:
My issue is: if you choose one of the occurrences on the legend you have this (choosing d):
I really would like that if you choose for example the d, the plot "returns" to a 100% stacked barplot, recalculating the percentage logically. Is it possible? If so, how?
Thanks in advance.
I'm trying to produce a histogram with ggplot's geom_histogram which colors the bars according to a gradient, and log10's them.
Here's the code:
library(ggplot2)
set.seed(1)
df <- data.frame(id=paste("ID",1:1000,sep="."),val=rnorm(1000),stringsAsFactors=F)
bins <- 10
cols <- c("darkblue","darkred")
colGradient <- colorRampPalette(cols)
cut.cols <- colGradient(bins)
df$cut <- cut(df$val,bins)
df$cut <- factor(df$cut,level=unique(df$cut))
Then,
ggplot(data=df,aes_string(x="val",y="..count..+1",fill="cut"))+
geom_histogram(show.legend=FALSE)+
scale_color_manual(values=cut.cols,labels=levels(df$cut))+
scale_fill_manual(values=cut.cols,labels=levels(df$cut))+
scale_y_log10()
gives:
whereas dropping the fill from the aesthetics:
ggplot(data=df,aes_string(x="val",y="..count..+1"))+
geom_histogram(show.legend=FALSE)+
scale_color_manual(values=cut.cols,labels=levels(cuts))+
scale_fill_manual(values=cut.cols,labels=levels(cuts))+
scale_y_log10()
gives:
Any idea why do the histogram bars differ between the two plots and to make the first one similar to the second one?
The OP is trying to produce a histogram with ggplot's geom_histogram which colors the bars according to a gradient...
The OP has already done the binning (with 10 bins) but is then calling geom_histogram() which does a binning on its own using 30 bins by default (see ?geomhistogram).
When geom_bar() is used instead together with cutinstead of val
ggplot(data = df, aes_string(x = "cut", y = "..count..+1", fill = "cut")) +
geom_bar(show.legend = FALSE) +
scale_color_manual(values = cut.cols, labels = levels(df$cut)) +
scale_fill_manual(values = cut.cols, labels = levels(df$cut)) +
scale_y_log10()
the chart becomes:
Using geom_histogram() with filled bars is less straightforward as can be seen in this and this answer to the question How to fill histogram with color gradient?