Ggplot: Line Plot for Discrete X-Axis

Making line plot with discrete x-axis in ggplot2

On this page they make the following modification df2$dose<-as.factor(df2$dose). You can try to modify your x-axis as df2$Concentration<-as.factor(df2$Concentration)

or like this:

ggplot(data=MasterTable, aes(x=factor(Concentration), y=Percentage, group=Time)) + 
geom_point() +
geom_line() +
facet_grid(Chemicals ~ Treatments)

How to make a line graph in ggplot with discrete x-axis?

I think you just need to pivot your data, order the days, and assign a grouping in the aesthetic:

library(tidyverse)

data_long <- data %>%
pivot_longer(cols = ends_with("Day"),
names_to = "day",
values_to = "count") %>%
mutate(day = as_factor(day))

ggplot(data = data_long, aes(x = day, y = count, group = CURRENT_STATUS)) +
geom_line(aes(color = CURRENT_STATUS, linetype = CURRENT_STATUS)) +
facet_wrap(~content_type)

Sample Image

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

Sample Image

Spacing on x-axis using ggplot2 discrete scale

When you plot categorical data on the x axis, you are "really" plotting at integer values x = 1, x = 2, etc, but with the text labels used in place of numbers. This is what allows you to put text annotations at x = 1 and x = 2.

However, the bars are bunched at the left because you have added two empty text annotations over to the right (at position x = 3 and x = 4). The plot has expanded right to accommodate them. Since they are empty anyway, you don't need them. Here is the plot without them:

CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
geom_col(fill = "lightsteelblue") +
geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
scale_x_discrete(labels = xlabels) +
xlab("") +
ylab("") +
theme(panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(size=20,color="black"),
axis.text.y = element_text(size=20, color="black"),
axis.title.x = element_text(size=25),
axis.title.y = element_text(size=25))+
annotate("text",x=1,y=0.84,label="A",size = 5) +
annotate("text",x=2,y=0.62,label="B",size = 5)

CTplot

Sample Image

And here it is with an empty annotation at x = 4:

CTplot + annotate("text",x = 4, y = 0.29, label = "", size = 5) 

Sample Image

To emphasise the point, let's see an empty annotation at x = 20:

CTplot + annotate("text",x = 20, y = 0.29, label = "", size = 5) 

Sample Image

As you can see, the x axis has had to expand to accommodate the invisible text annotation at x = 20.


If you want the bars a bit more spread out, you can do something like:

CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
geom_col(fill = "lightsteelblue", width = 0.6) +
geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
scale_x_discrete(labels = xlabels, expand = c(0.75, 0)) +
xlab("") +
ylab("") +
theme(panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(size=20,color="black"),
axis.text.y = element_text(size=20, color="black"),
axis.title.x = element_text(size=25),
axis.title.y = element_text(size=25))+
annotate("text",x=1,y=0.84,label="A",size = 5) +
annotate("text",x=2,y=0.62,label="B",size = 5)

CTplot

Sample Image


Data used (approximated from image in OP)

CTsum <- data.frame(Treatment = c("A", "B"), FI = c(0.71, 0.48), meanse = 0.1)

Plot rectangles using geom_rect with continous x-axis and discrete values in y-axis (R)

library(dplyr)
y_lab <- data %>%
distinct(y_end, y_start, info) %>%
mutate(y_mid = (y_end + y_start)/2)

ggplot(data, aes(xmin=x_start, xmax=x_end, ymin=y_start, ymax=y_end, fill=info)) +
geom_rect() +
scale_y_continuous(breaks = y_lab$y_mid, labels = y_lab$info)

Sample Image

Or using geom_tile:

ggplot(data, aes(x = (x_start + x_end)/2, y = info, fill=info, width = 1)) +
geom_tile()

Sample Image

ggplot - Manually Rearrange Order of X-Axis Levels

If I understand you correctly, you want to compare the FCC and FCNC points within each object type. In which case, it would be better to separate the object type into its own variable and FCC/FCNC into a different variable, since conceptually these are two different variables. To plot this, I might do something like this:

melted <- reshape2::melt(CON_FCC_summary)

tidyr::separate(melted, variable, into = c('Var1', 'Var2'), sep = '_') %>%
ggplot(aes(Var1, value)) +
geom_line(aes(group = 1)) +
geom_point(aes(color = Var1), size = 3) +
scale_color_brewer(palette = 'Set1', name = NULL) +
facet_grid(.~Var2, switch = 'x') +
theme_minimal(base_size = 16) +
theme(panel.spacing = unit(0, 'mm'),
strip.placement = 'outside',
axis.title.x = element_blank())

Sample Image

Can't get R to do a line between years

I would suggest just leaving the x-axis as continuous, rather than converting it to a factor. Simply provide the actual breaks you want for your x-axis, in this case, the years 2020:2022

ggplot(df, aes(Year,Quadrats,color=Ash)) + 
geom_point() + geom_line() +
scale_x_continuous(breaks=2020:2022)
theme_bw()

quadrats



Related Topics



Leave a reply



Submit