How to plot one variable in ggplot?
You can manually create an index vector with seq_along
.
library(ggplot2)
qplot(seq_along(iris$Sepal.Length), iris$Sepal.Length)
How to plot an equation changing value of one variable in R using ggplot
You can use stat_function
.
p <- function(age, u, k, N, m)
{
1 - (1 - (1 - (1 - u)^((365/4) * age))^k)^(N * m)
}
ggplot(data.frame(age = 1:100), aes(x = age)) +
stat_function(fun = p, args = list(u = 0.000005, k = 5, N = 8, m = 15000000)) +
labs(y = "p")
There was no variable u
defined in the question, but this is a logistic curve, and setting u
to 0.000005 puts the middle of your range at p = 0.5, so u
is presumably near this value.
How to plot multiple boxplots with a single variable each on ggplot2?
Bring your data in long format with
pivot_longer
fromtidyr
package (is intidyverse
)use
ggplot
fromggplot2
package (is also intidyverse
)geom_boxplot
andgeom_jitter
if needed.
library(tidyverse)
df %>%
mutate(id = row_number()) %>%
pivot_longer(
cols = starts_with("X"),
names_to = "names",
values_to = "values"
) %>%
ggplot(aes(x=names, y=values, fill=names))+
geom_boxplot() +
geom_jitter(aes(y=values))
How to plot only one column in ggplot?
You could create a row index using tibble::rownames_to_column
, then use that to plot along the x-axis, so that you get a similar result to plot(dat$value)
.
library(tidyverse)
dat %>%
rownames_to_column("ind") %>%
ggplot(aes(x = ind, y = value, color = name)) +
geom_point(size = 3) +
theme_bw()
Output
Or you can put the function directly into ggplot
:
ggplot(dat, aes(
x = rownames_to_column(dat)$rowname,
y = value,
color = name
)) +
geom_point(size = 3) +
theme_bw()
Or another option is to use row.names
:
ggplot(dat, aes(x = as.numeric(row.names(dat)), y = value, colour = name)) +
geom_point()
How can I plot just a single response type from two different variables using ggplot?
Convert from wide-to-long then plot:
library(dplyr)
library(tidyr)
pivot_longer(a, cols = 2:3) %>%
filter(value == "Y") %>%
ggplot(aes(x = value, y = hour, fill = name)) +
geom_dotplot(binaxis = "y", stackgroups = TRUE, binwidth = 1,
binpositions = "all", stackdir = "center")
ggplot2() plotting one variable against itself by factor?
Using tidyverse functions, you can do
library(dplyr)
library(tidyr)
library(ggplot2)
dd %>%
group_by(fac) %>%
mutate(id=1:n()) %>%
spread(fac, score) %>%
ggplot(aes(trial2, trial1)) +
geom_line()
Plot values with a certain order based on another column in ggplot
If you want to make the factor appears as in the data, refactor the variable with levels as unique(your_column)
, which will use the levels as they appear in the dataset.
Your points are ordered in the reversed order, just use scale_x_discrete(limits=rev)
to make it appear the correct way:
mydata %>%
mutate(Taxa = factor(Taxa, levels = unique(Taxa))) %>%
ggplot(aes(x = Taxa, y = Number)) +
geom_segment(aes(x = Taxa, xend = Taxa, y = 0, yend = Number), color = "grey") +
geom_point(size = 3, color = "darkorange") +
coord_flip() +
scale_x_discrete(limits=rev)
More thorough explanation:
Factor is a class and contains levels. ggplot
orders the factor variable in the plot according to the order of the levels (so not according to the order as they appear in the data). If you order the dataset, the order of the levels will remain the same, i.e., you have to explicitly reorder the levels to do that. So one way to reorder the levels is to use factor
and explicitly set the levels
in the order of the data using unique
. It may be a bit cumbersome, but that's the cleanest way to deal with factors.
Related Topics
Avoiding Type Conflicts with Dplyr::Case_When
R: How to Total the Number of Na in Each Col of Data.Frame
Read.CSV Is Extremely Slow in Reading CSV Files with Large Numbers of Columns
Using Geo-Coordinates as Vertex Coordinates in the Igraph R-Package
Identify Points Within Specified Distance in R
How to Change the Position of the Table of Contents in Rmarkdown
K-Means Clustering in R on Very Large, Sparse Matrix
About Gforce in Data.Table 1.9.2
Shiny: How to Adjust the Width of the Tabsetpanel
How to Group by All But One Columns
Using a Loop to Create Multiple Data Frames in R
Create Lagged Variable in Unbalanced Panel Data in R
How to Manually Create a Dendrogram (Or "Hclust") Object? (In R)