I need to realize an histogram in R. I add a picture to represent the desired results. I had tried to use both ggplot2 and the base function hist. I used this code (ggplot) to get the basic histogram, but I would like to add the option to set the x-axis as shown in the figure (exactly the same values). Can someone tell me how to do that?
My imput file DataLig2 contains a list of objects and for each of these is associated a value (N..of.similar..Glob.Sum...0.83..ligandable.pockets). I need to plot the frequencies of all the reported values. The lowest value is 1 and the highest is 28. There aren't values from 16 to 27 so I would like to skip thi range in my plot.
example of imput file:
Object;N..of.similar..Glob.Sum...0.83..ligandable.pockets
1b47_A_001;3
4re2_B_003;1
657w_H_004_13
1gtr_A_003;28
...
my script:
ggplot(dataLig2, aes(dataLig2$N..of.similar..Glob.Sum...0.83..ligandable.pockets, fill = group)) + geom_histogram(color="black") +
scale_fill_manual(values = c("1-5" = "olivedrab1",
"6-10" = "limegreen",
"11-28" = "green4"))
Can you also suggest a script with the hist base function to get the same graph (with spaced bars as in the figure shown)? Thank you!
Using ggplot, set x as factor, missing numbers as "...", and set to plot unused levels, see example:
library(ggplot2)
# reproducible example data
# where 8 and 9 is missing
set.seed(1); d <- data.frame(x = sample(c(1:7, 10), 100, replace = TRUE))
# add missing 8 and 9 as labels
d$x1 <- factor(d$x, levels = 1:10, labels = c(1:7, "...", "...", 10))
#compare
cowplot::plot_grid(
ggplot(d, aes(x)) +
geom_bar() +
ggtitle("before") +
scale_x_continuous(breaks = 1:10),
ggplot(d, aes(x = x1)) +
geom_bar() +
scale_x_discrete(drop = FALSE) +
ggtitle("after"))
Related
I'm struggling with ggplot2 and I've been looking for a solution online for several hours. Maybe one of you can give me a help? I have a data set that looks like this (several 100's of observations):
Y-AXIS
X-AXIS
SUBJECT
2.2796598
F1
1
0.9118639
F1
2
2.7111228
F3
3
2.7111228
F2
4
2.2796598
F4
5
2.3876401
F10
6
....
...
...
The X-AXIS is a continuous value larger than 0 (the upper limit can vary from data set to data set, but is typically < 100). Y-AXIS is a categorical variable with 10 levels. SUBJECT refers to an individual and, across the entire data set, each individual has exactly 10 observations, exactly 1 for each level of the categorical variable.
To generate a box plot, I used ggplot like this:
plot1 <- ggplot(longdata,
aes(x = X_axis, y = Y_axis)) +
geom_boxplot() +
ylim(0, 12.5) +
stat_summary(fun = "mean", geom = "point", shape = 2, size = 3, color = "purple")
That results in the boxplot I have in mind. You can check out the result here if you like: boxplot
So far so good. What I want to do next, and hopefully someone can help me, is this: for one specific SUBJECT, I want to plot a line for their 10 scores in the same figure. So on top of the boxplot. An example of what I have in mind can be found here: boxplot with data of one subject as a line. In this case, I simply assumed that the outliers belong to the same case. This is just an assumption. The data of an individual case can also look like this: boxplot with data of a second subject as a line
Additional tips on how to customize that line (colour, thikness, etc.) would also be appreciated. Many thanks!
library(ggplot2)
It is always a good idea to add a reproducible example of your data,
you can always simulate what you need
set.seed(123)
simulated_data <- data.frame(
subject = rep(1:10, each = 10),
xaxis = rep(paste0('F', 1:10), times = 10),
yaxis = runif(100, 0, 100)
)
In ggplot each geom can take a data argument, for your line just use
a subset of your original data, limited to the subject desired.
Colors and other visula elements for the line are simple, take a look here
ggplot() +
geom_boxplot(data = simulated_data, aes(xaxis, yaxis)) +
geom_line(
data = simulated_data[simulated_data$subject == 1,],
aes(xaxis, yaxis),
color = 'red',
linetype = 2,
size = 1,
group = 1
)
Created on 2022-10-14 with reprex v2.0.2
library(ggplot2)
library(dplyr)
# Simulate some data absent a reproducible example
testData <- data.frame(
y = runif(300,0,100),
x = as.factor(paste0("F",rep(1:10,times=30))),
SUBJECT = as.factor(rep(1:30, each = 10))
)
# Copy your plot with my own data + ylimits
plot1 <- ggplot(testData,
aes(x = x, y = y)) +
geom_boxplot() +
ylim(0, 100) +
stat_summary(fun = "mean", geom = "point", shape = 2, size = 3, color = "purple")
# add the geom_line for subject 1
plot1 +
geom_line(data = filter(testData, SUBJECT == 1),
mapping = aes(x=x, y=y, group = SUBJECT))
My answer is very similar to Johan Rosa's but his doesn't use additional packages and makes the aesthetic options for the geom_line much more apparent - I'd follow his example if I were you!
The grouping variable for creating a geom_violin() plot in ggplot2 is expected to be discrete for obvious reasons. However my discrete values are numbers, and I would like to show them on a continuous scale so that I can overlay a continuous function of those numbers on top of the violins. Toy example:
library(tidyverse)
df <- tibble(x = sample(c(1,2,5), size = 1000, replace = T),
y = rnorm(1000, mean = x))
ggplot(df) + geom_violin(aes(x=factor(x), y=y))
This works as you'd imagine: violins with their x axis values (equally spaced) labelled 1, 2, and 5, with their means at y=1,2,5 respectively. I want to overlay a continuous function such as y=x, passing through the means. Is that possible? Adding + scale_x_continuous() predictably gives Error: Discrete value supplied to continuous scale. A solution would presumably spread the violins horizontally by the numeric x values, i.e. three times the spacing between 2 and 5 as between 1 and 2, but that is not the only thing I'm trying to achieve - overlaying a continuous function is the key issue.
If this isn't possible, alternative visualisation suggestions are welcome. I know I could replace violins with a simple scatter plot to give a rough sense of density as a function of y for a given x.
The functionality to plot violin plots on a continuous scale is directly built into ggplot.
The key is to keep the original continuous variable (instead of transforming it into a factor variable) and specify how to group it within the aesthetic mapping of the geom_violin() object. The width of the groups can be modified with the cut_width argument, depending on the data at hand.
library(tidyverse)
df <- tibble(x = sample(c(1,2,5), size = 1000, replace = T),
y = rnorm(1000, mean = x))
ggplot(df, aes(x=x, y=y)) +
geom_violin(aes(group = cut_width(x, 1)), scale = "width") +
geom_smooth(method = 'lm')
By using this approach, all geoms for continuous data and their varying functionalities can be combined with the violin plots, e.g. we could easily replace the line with a loess curve and add a scatter plot of the points.
ggplot(df, aes(x=x, y=y)) +
geom_violin(aes(group = cut_width(x, 1)), scale = "width") +
geom_smooth(method = 'loess') +
geom_point()
More examples can be found in the ggplot helpfile for violin plots.
Try this. As you already guessed, spreading the violins by numeric values is the key to the solution. To this end I expand the df to include all x values in the interval min(x) to max(x) and use scale_x_discrete(drop = FALSE) so that all values are displayed.
Note: Thanks #ChrisW for the more general example of my approach.
library(tidyverse)
set.seed(42)
df <- tibble(x = sample(c(1,2,5), size = 1000, replace = T), y = rnorm(1000, mean = x^2))
# y = x^2
# add missing x values
x.range <- seq(from=min(df$x), to=max(df$x))
df <- df %>% right_join(tibble(x = x.range))
#> Joining, by = "x"
# Whatever the desired continuous function is:
df.fit <- tibble(x = x.range, y=x^2) %>%
mutate(x = factor(x))
ggplot() +
geom_violin(data=df, aes(x = factor(x, levels = 1:5), y=y)) +
geom_line(data=df.fit, aes(x, y, group=1), color = "red") +
scale_x_discrete(drop = FALSE)
#> Warning: Removed 2 rows containing non-finite values (stat_ydensity).
Created on 2020-06-11 by the reprex package (v0.3.0)
I am new to R and have been trying for a few days to plot histogram / bar chart to view the trend. I have this categorical variable : countryx and coded it into 1,2,3.
I have tried these 2 scripts below and got error messages as follows :
Output 1: blank chart with x and y axis, no stack/bar trend
qplot(DI$countryx,geom = "histogram",ylab = "count",
xlab = "countryx",binwidth=5,colour=I("blue"),fill=I("wheat"))
Output 2: error message- ggplot2 doesn't know how to deal with data of class integer
ggplot(DI$countryX, aes(x=countryx))
+ geom_bar(aes(y=count), stat = "count",position ="stack",...,
width =5,aes=true)
Appreciate for all advice.
Thank you very much for your help!
Multiple problems with your code. ggplot takes a dataframe, not a vector, but you're supplying a vector. Try this
ggplot(DI, aes(x=countryx, y = count)) + geom_col(width = 5)
As #yeedle mentioned you need a data.frame (maybe use as.data.frame)
How about:
library(ggplot2)
df <- data.frame(countryx = rep(1:3), count = rbinom(3,10,0.3))
p <- ggplot2::ggplot(df, aes(x = countryx, y = count)) + ylab("count")
p + geom_col(aes(x = countryx, fill = factor(countryx)))
I am trying to plot stacked bars using ggplot() and geom_bar()
Sample data (Titanic Kaggle question):
PassengerId Survived Age
1 0 25
2 1 20
3 1 40
4 0 10
I am trying to show stacked bars of survival and death for each age range (I have divided age into bins). Plot is not visible when I execute the command. and when I add print() function, I get the error as
Error: No layers in plot
Please tell if there is anything I am missing out here ?
breaks <- seq(min(train$Age), max(train$Age), 10)
p <- ggplot(train, aes(x=train$Age, y=length(train$PassengerId)), xlab = "age", ylab = "count", main = "survival",
fill = Survived + geom_bar(stat = "identity", bin = breaks))
print(p)
"train" is object in which I stored the data.
apart from Pascal's hint, you may want to:
Use factors for colored column (Survived)
Use different geom_bar() and aes() params as suggested
Code:
train <- data.frame(PassengerId = 1:4, Survived = factor(c(0,1,1,0)), Age = c(25, 20, 40, 10))
BIN.WIDTH <- 10
my.breaks <- seq(from = min(train$Age), to = max(train$Age) + BIN.WIDTH, by = BIN.WIDTH)
ggplot(train, aes(Age, fill = Survived)) + geom_bar(breaks = my.breaks)
Plot:
There are a few issues here:
First you should remove the rows where age is NA, otherwise you can't create a sequence.
train<-train[!is.na(train$Age),]
Then you should change your y value to train$Survived (why did you use length(train$PassengerId)? - it doesn't display anything)
The thing that #Pascal mentioned is also correct: You have to put + geom_bar(stat="identity", bin=breaks) outside.
and you need to add the axes and title differently in ggplot.
This is the complete working code:
train<-train[!is.na(train$Age),]
breaks <- seq(min(train$Age), max(train$Age), 10)
p <- ggplot(train, aes(x=train$Age, y=train$Survived),
fill = Survived)+ geom_bar(stat="identity", bin=breaks)
p <- p+labs(x="age", y="count")
p <- p+theme(plot.title= element_text("survival"))
print(p)
Results in this graph:
I'm using the following code to produce three different histograms in the same graph. However, I was wondering, if it is possible to separate the three different histograms in three different graphs underneath each other with the same scaling on the x-axis for all three graphs. As an alternative I thought about turning the three histograms into densities and still have them in the same graph.
require(ggplot2)
require(reshape2)
set.seed(1)
df <- data.frame(x = rnorm(n = 1000, mean = 2, sd = 0.2),
y = rnorm(n = 1000, mean = 2),
z = rnorm(n = 1000, mean = 2))
ggplot(melt(df), aes(value, fill = variable)) + geom_histogram(position = "dodge")+ scale_fill_manual(values = c('red','black','green'))
Thanks.
Try this:
gg <- melt(df)
ggplot(gg) + geom_bar(aes(x=value,fill=variable)) + facet_grid(variable~., scale.)
The function melt(...) transforms your data from "wide" format (values in different columns) to "long" format (valuesin one column, with an extra column indicating which value goes with which variable. This is a preferred format for ggplot. Then facet_grid(...) puts the different variables (x,y,z) into different graphs (or panels).
Use this for densities:
ggplot(gg) +
stat_density(aes(x=value, color=variable),geom="line",position="dodge")