class_day <- c(1:10)
control_group <- c(67,72,69,81,73,66,71,72,77,71)
A_treatment_group <- c(NA,72,77,81,73,85,69,73,74,77)
B_treatment_group <- c(NA,66,68,69,67,72,73,75,79,77)
class.df<-data.frame(class_day, control_group, A_treatment_group, B_treatment_group)
I tried to convert vecotrs to a table but I am not sure how to include three categories in one plot.
How can I get a scatter plot with three different colors?
I would like to set x-axis as class_day above and y axis as scores.
First, A cleaner way to make a dataframe without the intermediate variables.
You can make this type of chart by pivoting the data into "long" form:
class.df<-data.frame(class_day = c(1:10),
control_group = c(67,72,69,81,73,66,71,72,77,71),
A_treatment_group = c(NA,72,77,81,73,85,69,73,74,77),
B_treatment_group = c(NA,66,68,69,67,72,73,75,79,77) )
library(tidyverse)
class.df %>%
pivot_longer(!class_day) %>%
ggplot(aes(x=class_day, y=value, color=name))+
geom_point()
Here is a version with ggscatter from ggpubr:
library(ggpubr)
library(tidyverse)
class.df %>%
pivot_longer(-class_day,
names_to= "group",
values_to = "score") %>%
ggscatter(x = "class_day", y = "score", color = "group",
palette = c("#00AFBB", "#E7B800", "#FC4E07"))
I want to create a bubble chart like this:
sample chart
I want the y axis for project stage, and x axis for nothing. The size of the circle could be the number of projects and the color can be the status.
I think it should be a bubble chart of point chart, so I add solid value for x axis to set x position for all circle. But I don't have an idea how to set y axis for them.
This is my data sample:
test<- data.frame(
stage = c("Designment","Designment","Development","Development","Development","Go Live","Go Live","Requirement","Requirement","Test","Test","UAT","UAT"),
status=c("At Risk","On Track","At Risk","On Hold","On Track","Completed","On Track","On Hold","On Track","Completed","On Track","Completed","On Track"),
total=c("1","9","1","2","23","25","2","9","11","1","6","1","1"),
x=c("1.00114754443462","1.01564186197464","0.998163923987934","1.00428226961525","1.00490918326707","0.996299554522548","0.979025253250673","0.996035528009065","0.997494036022247","0.998396639432905","0.987719363426524","0.990205701650603","1.01609094768587"),
y=c("0.444705037188138","0.628219900896655","0.538614704467962","0.587639745611629","0.289049383370184","0.568458079545019","0.302063527274047","0.520840349575939","0.687424167003373","0.564338710368995","0.593610161950449","0.662218219412209","0.370813953512016")
)
Is there any solution to this kind of chart?
Using ggplot2 you can:
library(palmerpenguins)
library(tidyverse)
penguins %>%
group_by(species, sex) %>%
summarise(
body_mass_g = mean(body_mass_g),
n = n(),
.groups = "drop"
) %>%
drop_na() %>%
ggplot(aes(x = body_mass_g, y = species, colour = sex, size = n)) +
geom_point(alpha = 0.5) +
geom_text(aes(label = n), size = 3, colour = "white") +
scale_size(range = c(15, 30), guide = NULL)
Created on 2022-08-15 by the reprex package (v2.0.1)
There are a few different ways to create a bubble chart in R. One way is to use the "ggplot2" package. Another way is to use the "plotly" package.
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:
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)