How to Plot One Variable in Ggplot

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)

Sample Image

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")

Sample Image

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?

  1. Bring your data in long format with pivot_longer from tidyr package (is in tidyverse)

  2. use ggplot from ggplot2 package (is also in tidyverse)

  3. geom_boxplot and geom_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))

Sample Image

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

Sample Image

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")

Sample Image

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()

Sample Image

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)

Sample Image


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



Leave a reply



Submit