Why does the command "group" in ggplot doesn't work? - r

library (tidyverse)
library (datarium)
data("anxiety", package = "datarium")
anxiety <- anxiety %>%
rename(groupes = group)
anxiety1 <- anxiety %>%
pivot_longer (c("t3", "t2", "t1"), names_to = "moment",
values_to = "valeur") %>%
print ()
ggplot (data = anxiety1, aes (x = groupes, y = valeur,
colour = moment, group = moment)) +
geom_line () +
geom_point ()
The command "group" in ggplot2 doesn't work and give an awful chart.

If you keep groupes on the x axis, there is no way to have lines going across the page, since each individual id only belongs to a single group.
The lines would therefore all be vertical.
In your version, you have joined the dots of all individuals at a particular moment by setting moment as the grouping variable. This will join all the dots for each color in each moment, then zig-zag to the next point on the x axis, which looks ugly. However, this is exactly what setting group = moment is asking ggplot to do.
Instead, it makes more sense to have moment on the x axis, colour by groupes, and group by id:
ggplot(data = anxiety1,
aes (x = moment, y = valeur, colour = groupes, group = id)) +
geom_line() +
geom_point()

I would follow #Allan Cameron suggestion in this case. I managed to find the error. Basically is the way you save your result (name of the tibbles). I inserted the comments where I found the errors.
Sample code:
library (tidyverse)
library (datarium)
data("anxiety", package = "datarium")
anxiety1 <- anxiety %>% #name as anxiety1
rename(groupes = group)
anxiety2 <- anxiety1 %>% # change name to anxiety2
pivot_longer (c("t3", "t2", "t1"), names_to = "moment",
values_to = "valeur") %>%
print ()
ggplot (data = anxiety2, aes (x = groupes, y = valeur, colour = moment, group = moment)) + # change to anxiety 2
geom_line () +
geom_point ()
Sample plot:

Related

Subset data using rangeslider in plotly for R

I am trying to make a scatterplot with plotly for R where the dots are connected in order using geom_path.
Now I would like to add a rangeslider where the user can select a date range.
Here is something similar using Python: Youtube Code
...but I don't need any recalculation of means or something like that, I just want to filter based on column i.
Unfortunately, I am having trouble doing that in R plotly.
This is my attempt, but I don't know how to tell plotly to subset the data using the i column:
library(tidyverse)
library(plotly)
p <- mtcars %>%
mutate(i = 1:nrow(mtcars)) %>%
ggplot(aes(x = mpg, y = wt))+
geom_path(size = 0.5) +
geom_point(aes(color = i), size = 3)
ggplotly(p) %>%
layout(
xaxis = list(rangeslider = list())
)

create plot in ggplot for each unique value in a row in r

I have a dataframe like this:
library(tidyverse)
my_data <- tibble(name = c("Justin", "Janet", "Marisa"),
x = c(100, 50, 75),
y = c(2, 3, 6))
Each name is unique, and I want to make a bar graph for each person without having to do it line by line. I also want to save each plot as a unique object because I'll be inputting it into a power point using the officer package. Last, the names won't always be the same, but each name will always be unique.
For instance, I want one plot for Janet, one plot for Justin, and one plot for Marisa. I don't want them faceted but instead as their own objects.
Any thoughts?
We can get the data in long format first and for each individual name create the plot.
library(tidyverse)
long_data <- my_data %>% tidyr::pivot_longer(cols = -name, names_to = 'col')
plots_list <- map(unique(my_data$name), ~long_data %>%
filter(name == .x) %>%
ggplot() + aes(name, value, fill = col) +
geom_bar(stat = 'identity', position = 'dodge') +
scale_fill_manual(values = c('red', 'blue')) +
ggtitle(paste0('Plot for ', .x)))
This will return list of plots where individual plots can be accessed via plots_list[[1]], plots_list[[2]] etc.
plots_list[[1]]

How can I integrate multiple distinctive plot for file bar with common label and legend?

I am trying to integrate multiple plot for file bar, where each file has two stack bar plot, and plot easily confused without wrap them into one single grid. However, I intend to improve the result of this plot that add common legend and label for whole graph. I tried several away to integrate multiple plot for each file in more clear way, so putting these into one grid for file bar could be more elegant and easy to understand the output. I confused about the answer from several similar post in SO, bit of new with ggplot2, I couldn't produce my desired plot at the end. Can any give me possible idea to improve this current plot in better way ? How can I add common label and legend for multiple graph ? Any idea please ?
reproducible data.frame :
Qualified <- list(
hotan = data.frame( begin=c(7,13,19,25,31,37,43,49,55,67,79,103,31,49,55,67),
end= c(10,16,22,28,34,40,46,52,58,70,82,106,34,52,58,70),
pos.score=c(11,19,8,2,6,14,25,10,23,28,15,17,6,10,23,28)),
aksu = data.frame( begin=c(12,21,30,39,48,57,66,84,111,30,48,66,84),
end= c(15,24,33,42,51,60,69,87,114,33,51,69,87),
pos.score=c(5,11,15,23,9,13,2,10,16,15,9,2,10)),
korla = data.frame( begin=c(6,14,22,30,38,46,54,62,70,78,6,30,46,70),
end=c(11,19,27,35,43,51,59,67,75,83,11,35,51,75),
pos.score=c(9,16,12,3,20,7,11,13,14,17,9,3,7,14))
)
unQualified <- list(
hotan = data.frame( begin=c(21,33,57,69,81,117,129,177,225,249,333,345,33,81,333),
end= c(26,38,62,74,86,122,134,182,230,254,338,350,38,86,338),
pos.score=c(7,34,29,14,23,20,11,30,19,17,6,4,34,23,6)),
aksu = data.frame( begin=c(13,23,33,43,53,63,73,93,113,123,143,153,183,33,63,143),
end= c(19,29,39,49,59,69,79,99,119,129,149,159,189,39,69,149),
pos.score=c(5,13,32,28,9,11,22,12,23,3,6,8,16,32,11,6)),
korla = data.frame( begin=c(23,34,45,56,67,78,89,122,133,144,166,188,56,89,144),
end=c(31,42,53,64,75,86,97,130,141,152,174,196,64,97,152),
pos.score=c(3,10,19,17,21,8,18,14,4,9,12,22,17,18,9))
)
I am categorzing data and get multiple plot in this way (mainly influenced by #Jake Kaupp's idea) :
multi_plot <- function(x) {
p1 <- ggplot(x, aes(x = group)) +
geom_bar(aes(fill = elm), color = "black")
p2 <- ggplot(distinct(x), aes(x = elm)) +
geom_bar(aes(fill = group), color = "black")
arrangeGrob(p1, p2,nrow = 1, top = unique(x$list))
}
singleDF <-
bind_rows(c(Qualified = Qualified, Unqualified = unQualified), .id = "id") %>%
tidyr::separate(id, c("group", "list")) %>%
mutate(elm = ifelse(pos.score >= 10, "valid", "invalid")) %>%
arrange(list, group, desc(elm))
plot_data <- singleDF %>%
split(.$list) %>%
map(~split_plot(.x))
grid.arrange(grobs = plot_data, nrow = 1)
I am trying to integrate multiple plot for file bar with common label and common legend position. In terms of common legend, I intend to call X axis as sample, Y axis as observation; in terms of common legend position, I intend to indicate legend at right side of plot (only four common legend).
EDIT:
In my desired output plot, stack bar plot of group and elm must be put in one single grid for file bar. Regarding whole graph, pursuing common label and legend is desired.
How can I achieve my desired output ? What change has to be taken in original implementation ? sorry for this simple question in SO. Thanks in advance
combinedDF <-
bind_rows(mutate(singleDF, x = group, fill = elm),
mutate(singleDF, x = elm, fill = group) %>% distinct()) %>%
mutate(x = factor(x, levels = c('invalid', 'valid', 'Unqualified', 'Qualified')),
fill = factor(fill, levels = c('invalid', 'valid', 'Unqualified', 'Qualified')))
ggplot(combinedDF, aes(x = x, fill = fill)) +
geom_bar() +
geom_text(aes(label = ..count..), stat = 'count', position = 'stack') +
facet_grid(~list)

How to plot the mean of a single factor in a barplot with

I'm having trouble to create a figure with ggplot2.
In this plot, I'm using geom_bar to plot three factors. I mean, for each "time" and "dose" I'm plotting two bars (two genotypes).
To be more specific, this is what I mean:
This is my code till now (Actually I changed some settings, but I'm presenting just what is need for):
ggplot(data=data, aes(x=interaction(dose,time), y=b, fill=factor(genotype)))+
geom_bar(stat="identity", position="dodge")+
scale_fill_grey(start=0.3, end=0.6, name="Genotype")
Question: I intend to add the mean of each time using points and that these points are just in the middle of the bars of a certain time. How can I proceed?
I tried to add these points using geom_dotplot and geom_point but I did not succeed.
library(dplyr)
time_data = data %>% group_by(time) %>% summarize(mean(b))
data <- inner_join(data,time_data,by = "time")
this gives you data with the means attached. Now make the plot
ggplot(data=data, aes(x=interaction(dose,time), y=b,fill=factor(genotype)))+
geom_bar(stat="identity", position="dodge")+
scale_fill_grey(start=0.3, end=0.6, name="Genotype")+
geom_text(aes(b),vjust = 0)
You might need to fiddle around with the argument hjust and vjust in the geom_text statement. Maybe the aes one too, I didn't run the program so I don't know.
It generally helps if you can give a reproducible example. Here, I made some of my own data.
sampleData <-
data.frame(
dose = 1:3
, time = rep(1:3, each = 3)
, genotype = rep(c("AA","aa"), each = 9)
, b = rnorm(18, 20, 5)
)
You need to calculate the means somewhere, and I chose to do that on the fly. Note that, instead of using points, I used a line to show that the mean is for all of those values. I also sorted somewhat differently, and used facet_wrap to cluster things together. Points would be a fair bit harder to place, particularly when using position_dodge, but you could likely modify this code to accomplish that.
ggplot(
sampleData
, aes(x = dose
, y = b
, fill = genotype)
) +
geom_bar(position = "dodge", stat = "identity") +
geom_hline(data =
sampleData %>%
group_by(time) %>%
summarise(meanB = mean(b)
, dose = NA, genotype = NA)
, aes(yintercept = meanB)
, col = "black"
) +
facet_wrap(~time)

Plotting a bar graph in R

Here is a snapshot of data:
restaurant_change_sales = c(3330.443, 3122.534)
restaurant_change_labor = c(696.592, 624.841)
restaurant_change_POS = c(155.48, 139.27)
rest_change = data.frame(restaurant_change_sales, restaurant_change_labor, restaurant_change_POS)
I want two bars for each of the columns indicating the change. One graph for each of the columns.
I tried:
ggplot(aes(x = rest_change$restaurant_change_sales), data = rest_change) + geom_bar()
This is not giving the result the way I want. Please help!!
So ... something like:
library(ggplot2)
library(dplyr)
library(tidyr)
restaurant_change_sales = c(3330.443, 3122.534)
restaurant_change_labor = c(696.592, 624.841)
restaurant_change_POS = c(155.48, 139.27)
rest_change = data.frame(restaurant_change_sales,
restaurant_change_labor,
restaurant_change_POS)
cbind(rest_change,
change = c("Before", "After")) %>%
gather(key,value,-change) %>%
ggplot(aes(x = change,
y = value)) +
geom_bar(stat="identity") +
facet_grid(~key)
Which will produce:
Edit:
To be extra fancy e.g. make it so that the order of x-axis labels goes from "Before" to "After", you can add this line: scale_x_discrete(limits = c("Before", "After")) to the end of the ggplot function
Your data are not formatted properly to work well with ggplot2, or really any of the plotting packages in R. So we'll fix your data up first, and then use ggplot2 to plot it.
library(tidyr)
library(dplyr)
library(ggplot2)
# We need to differentiate between the values in the rows for them to make sense.
rest_change$category <- c('first val', 'second val')
# Now we use tidyr to reshape the data to the format that ggplot2 expects.
rc2 <- rest_change %>% gather(variable, value, -category)
rc2
# Now we can plot it.
# The category that we added goes along the x-axis, the values go along the y-axis.
# We want a bar chart and the value column contains absolute values, so no summation
# necessary, hence we use 'identity'.
# facet_grid() gives three miniplots within the image for each of the variables.
ggplot2(rc2, aes(x=category, y=value, facet=variable)) +
geom_bar(stat='identity') +
facet_grid(~variable)
You have to melt your data:
library(reshape2) # or library(data.table)
rest_change$rowN <- 1:nrow(rest_change)
rest_change <- melt(rest_change, id.var = "rowN")
ggplot(rest_change,aes(x = rowN, y = value)) + geom_bar(stat = "identity") + facet_wrap(~ variable)

Resources