Using `geom_line()` with X axis being factors
If I understand the issue correctly, specifying group=1
and adding a stat_summary()
layer should do the trick:
ggplot(hist, aes(x=weekday, y=counts, group=1)) +
geom_point(stat='summary', fun.y=sum) +
stat_summary(fun.y=sum, geom="line")
ggplot2: Factor for x axis with geom_line doesn't work
To plot a line graph with factors on the x-axis, you need to use group
in addition to color
...
ggplot(data, aes(expt, value, group=var, color=var)) + geom_line()
Gives me this output:
How to use + geom_line() with a categorical x-variable and quantitative y-variable
You can convert var_names
into a factor and set the levels in the order of appearance (otherwise it will be assigned alphanumerically and the x axis will be out of order). Then just add series_type
to the group parameter in the plot.
df2 <- gather(df1, key = "series_type", value = "value", c(2:4)) %>%
mutate(var_names = factor(var_names, levels = unique(var_names)))
ggplot(df2, aes(x = var_names, y = value, color = series_type, group = series_type)) + geom_line() + geom_point()
geom_line() with x as factor and a grouping variable for color
You need to group by the interaction of ATB and status, otherwise you are not correctly telling ggplot which points to connect:
ggplot(d, aes(x=season, y = n)) +
geom_point(aes(color = ATB)) +
geom_line(aes(color = ATB, linetype = status,
group = interaction(ATB, status))) +
scale_linetype_manual(values = c(2, 1))
ggplot dismisses x axis factor levels
This is made more difficult by your weeks being factors rather than numbers from 1 to 53 (you could always make the x axis numeric and label it with text which would fix the problem). Anyway, the reason this reordering is happening is because not all the factor levels of week_nr
appear in the subset cat == "1"
. The unused factor levels are dropped, which triggers a re-ordering. There are a couple ways to fix this:
- Add
scale_x_discrete(drop = FALSE)
- Move the
geom_point
call before thegeom_line
call, since the dataset used for the first geom drawn determines the levels that are used.
ggplot(x, aes(week_nr, values, group = year)) +
geom_line(data = x %>% filter(cat == "1"), color = "red") +
geom_point(data = x %>% filter(cat == "0"), color = "grey") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_x_discrete(drop = FALSE)
Draw geom_line by y axis values instead of x axis
arrange
the data before plotting to get the required line order.
library(dplyr)
library(ggplot2)
dat %>%
arrange(transect, res, height) %>%
ggplot(aes(x=val, y=height,
color=factor(res), shape=plat), size=2) +
geom_point() +
scale_shape_manual(values=c(1, 4)) +
geom_path(position="identity") +
facet_wrap(~transect, ncol=4) +
labs(x="Gap fraction", y="Height aboveground (cm)",
color="Voxel size (m)", shape = "Platform") +
scale_y_continuous(breaks = c(140,400,800,1200)) +
theme_bw() +
theme(legend.position="top")
Why group=1 is not linking discrete variables together using geom_line?
You could set group
in your line to the variable name
. I set an alpha for the error bars to make the plot more clear. You can use this code:
ggplot(df, aes(x = cohort, y = est,color=name)) +
geom_point(position=position_dodge(width=0.0)) +
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0,position=position_dodge(width=0.0), alpha =0.5) +
geom_line(aes(x = cohort, y = est, group = name))
Output:
geom_line with group at x axis in R
As highlighted in the comments, your data doesn't have an "X" variable. This would typically be something like time or location. Alternatively, manually add one based on the grouped row number. To add the legend, include a mappable value like colour in the aesthetics.
df = df %>%
pivot_longer(-c(ID)) %>%
group_by(ID) %>%
mutate(row = row_number())
df %>%
ggplot(aes(x = row, y = value, group = ID, colour = ID)) +
geom_line(size = 1) +
labs(x = "ID",y = "value")
Related Topics
Insert Picture/Table in R Markdown
How to Remove Unicode <U+00A6> from String
Hosting and Setting Up Own Shiny Apps Without Shiny Server
Is There a Better Alternative Than String Manipulation to Programmatically Build Formulas
How to Convert R Markdown to HTML? I.E., What Does "Knit HTML" Do in Rstudio 0.96
How to Connect R with Access Database in 64-Bit Window
How to Calculate Combination and Permutation in R
Center X and Y Axis with Ggplot2
How to Change Library Location in R
Convert Named Character Vector to Data.Frame
Get Last Row of Each Group in R
Avoid Clipping of Points Along Axis in Ggplot