ggplot histogram code issues - r

Below there is a code producing a histogram using ggplot:
ggplot(res, aes(x=TOPIC,y=count)) +
scale_y_continuous(expand=c(0,0)) +
geom_bar(stat='identity') +
theme(axis.title.x=element_blank()) +
theme_bw(base_size = 16) +
ggtitle("Peer-reviewed papers per topic")
res<-structure(list(TOPIC = structure(c(20L, 18L, 21L, 3L, 9L, 4L,
7L, 8L, 17L, 27L, 29L, 2L, 24L, 30L, 16L, 28L, 10L, 22L, 12L,
5L, 1L, 19L, 6L, 25L, 11L, 23L, 14L, 15L, 13L, 26L), .Label = c("ANA",
"BEH", "BOUND", "CC", "DIS", "EVO", "HAB", "HABP", "HARV", "HWC",
"ISSUE", "METH", "MINOR", "PA", "PEFF", "PHYS", "POLL", "POPABU",
"POPGEN", "POPSTAT", "POPTR", "PRED", "PROT", "REPEC", "RESIMP",
"REV", "SHIP", "TEK", "TOUR", "ZOO"), class = "factor"), count = c(9,
7, 13, 5, 23, 35, 27, 5, 118, 0, 9, 22, 29, 46, 27, 12, 9, 44,
70, 40, 24, 19, 26, 2, 4, 17, 4, 10, 86, 31)), .Names = c("TOPIC",
"count"), row.names = c(NA, -30L), class = "data.frame")
The topics have names like "Population status" (POPSTAT), "Population trend" (POPTREND) etc.
There are issues I need help to solve:
1. The theme(axis.title.x=element_blank()) does not remove the x axis title.
2. The scale_y_continuous(expand=c(0,0)) puts the bar on the level of
the x-axis, which I need, but I need some expansion on the upper part
of the y-axis. How do I achieve that?
3. My labels from the data frame are abbreviations. How do I replace
these with new tick labels?
4. I need to turn the x-axis labels 90°.

Use labs(x = NULL)
Do you mean ylim? Using this, you can set the limits of the y-axis. Or use scale_y_continuous(limits = c(0, <upperLimit>), expand = c(0, 0))
Use a discrete scale with labels: scale_x_discrete(labels = <axis-labels>, limits = c(1:length(axis-labels))
theme(axis.text.x = element_text(angle = 90))

Related

not enough 'x' or 'y' observations in ggplot2 for t-tests or Wilcoxon test

I'm able to run a t-test or a Wilcoxon test on the data with no warnings, but I get an error when I try to plot in with ggpubr 's function stat_pvalue_manual() in ggplot2
The t-test works fine:
### running a t-test (no problem):
t.test(SUJ_PRE ~ AMOSTRA, data = data, exact = F)
But I get the error in stat_pvalue_manual:
### trying to plot it:
data %>%
ggplot(., aes(x = AMOSTRA, y = SUJ_PRE)) +
stat_boxplot(geom = "errorbar",
width = 0.15) +
geom_boxplot(aes(fill = AMOSTRA), outlier.colour = "#9370DB", outlier.shape = 19,
outlier.size= 2, notch = T) +
scale_fill_manual(values = c("PB" = "#E6E6FA", "PE" = "#CCCCFF"),
label = c("PB" = "PB", "PE" = "PE"),
name = "Amostra:") +
stat_summary(fun = mean, geom = "point", shape = 20, size= 5, color= "#9370DB") +
stat_pvalue_manual(data %>%
t_test(SUJ_PRE ~ AMOSTRA) %>%
add_xy_position(),
label = "t = {round(statistic, 2)}, p = {p}")
Error in `mutate()`:
! Problem while computing `data = map(.data$data, .f, ...)`.
Caused by error in `t.test.default()`:
! not enough 'x' observations
With SUJ_PRE I'm getting the error with 'x' , but I've also got the 'y' message with another variable too. Any thoughts on that? I've seem some similar questions, but I couldn't solve my problem. Thanks in advance.
data:
> dput(data)
structure(list(ID = c("COPA_M1B", "COPA_M1C", "COPA_M2B", "COPA_M2C",
"COPA_M3C", "COPA_M3A", "COPA_M3B", "COPA_H1A", "COPA_H1B", "COPA_H2A",
"COPA_H2B", "COPA_H2C", "COPA_H3A", "COPA_H3B", "NI_M1B", "NI_M1C",
"NI_M2A", "NI_M2B", "NI_M3C", "NI_M3A", "NI_M3B", "NI_H1A", "NI_H1B",
"NI_H1C", "NI_H2B", "NI_H2C", "NI_H3A", "NI_H3C", "CACEM_M1A",
"CACEM_M1B", "CACEM_M1C", "CACEM_M2B", "CACEM_M3B", "CACEM_H1B",
"CACEM_H1C", "CACEM_H2A", "CACEM_H2C", "OEIRAS_M1B", "OEIRAS_M1C",
"OEIRAS_M2B", "OEIRAS_M3B", "OEIRAS_M3C", "OEIRAS_H1B"), SUJ_PRE = c(25,
40, 56, 49, 47, 38, 58, 38, 42, 71, 43, 46, 74, 43, 45, 35, 70,
33, 45, 53, 50, 59, 62, 41, 35, 43, 40, 21, 23, 33, 35, 21, 36,
15, 31, 19, 31, 20, 22, 20, 19, 21, 25), AMOSTRA = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("PB", "PE"
), class = "factor")), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -43L), groups = structure(list(
ID = c("CACEM_H1B", "CACEM_H1C", "CACEM_H2A", "CACEM_H2C",
"CACEM_M1A", "CACEM_M1B", "CACEM_M1C", "CACEM_M2B", "CACEM_M3B",
"COPA_H1A", "COPA_H1B", "COPA_H2A", "COPA_H2B", "COPA_H2C",
"COPA_H3A", "COPA_H3B", "COPA_M1B", "COPA_M1C", "COPA_M2B",
"COPA_M2C", "COPA_M3A", "COPA_M3B", "COPA_M3C", "NI_H1A",
"NI_H1B", "NI_H1C", "NI_H2B", "NI_H2C", "NI_H3A", "NI_H3C",
"NI_M1B", "NI_M1C", "NI_M2A", "NI_M2B", "NI_M3A", "NI_M3B",
"NI_M3C", "OEIRAS_H1B", "OEIRAS_M1B", "OEIRAS_M1C", "OEIRAS_M2B",
"OEIRAS_M3B", "OEIRAS_M3C"), .rows = structure(list(34L,
35L, 36L, 37L, 29L, 30L, 31L, 32L, 33L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 6L, 7L, 5L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 15L, 16L, 17L, 18L, 20L,
21L, 19L, 43L, 38L, 39L, 40L, 41L, 42L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -43L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE))
Your data frame is grouped by ID, and rstatisx::t_test will honor the groupings in a data frame. This means some groups only have a single member, which of course causes an error.
The solution is simply to ungroup your data frame when using it inside stat_pvalue_manual:
data %>%
ggplot(aes(x = AMOSTRA, y = SUJ_PRE)) +
stat_boxplot(geom = "errorbar",
width = 0.15) +
geom_boxplot(aes(fill = AMOSTRA), outlier.colour = "#9370DB",
outlier.shape = 19,
outlier.size= 2, notch = T) +
scale_fill_manual(values = c("PB" = "#E6E6FA", "PE" = "#CCCCFF"),
label = c("PB" = "PB", "PE" = "PE"),
name = "Amostra:") +
stat_summary(fun = mean, geom = "point", shape = 20, size= 5,
color = "#9370DB") +
stat_pvalue_manual(data %>% ungroup() %>%
t_test(SUJ_PRE ~ AMOSTRA) %>%
add_xy_position(),
label = "t = {round(statistic, 2)}, p = {p}")

ggplot: how to get a semi-transparent fill in a geom_rect with a color different from other geoms? fill=alpha() does not seem to work

I am trying to visualize surgical procedures before and after Covid-19:
As you can see, I have a geom_rect() colored differently than the geom_point().
I want the blue color and fill in the geom_rect() to be semi-transparent, something like fill = alpha("#2C77BF", .5)). However, when using the script below, the alpha-part does not work.
How can I obtain the semi-transparency of geom_rect() for a color different from those specified for geom_point()?
ggplot(b,
aes(x = cons_week, y = n, color = corona, fill = corona)) +
geom_point(size = 5, shape = 21) +
geom_smooth(se = F, method = lm, color = "black", show.legend = F) +
geom_smooth(lty = 2, show.legend = F) +
geom_segment(aes(x = 167, xend = 167, y = 2.5, yend = 25),
color = "red", size = 1) +
geom_rect(aes(xmin = 1, xmax = 30,
ymin = 0, ymax = 5),
color = "#2C77BF",
fill = alpha("#2C77BF", .5)) +
scale_color_manual(name = "",
values = c("#8B3A62", "#6DBCC3"),
labels = c("COVID-19", "Normal"),
guide = guide_legend(reverse=TRUE)) +
scale_fill_manual(name = "",
values = alpha(c("#8B3A62", "#6DBCC3"), .25),
labels = c("COVID-19", "Normal"),
guide = guide_legend(reverse=TRUE)) +
scale_x_continuous(name = "",
breaks = seq(0, 210, 12)) +
scale_y_continuous(name = "",
breaks = seq(0, 30, 5), limits = c(0, 30)) +
theme(axis.title.y = element_text(color = "grey20",
size = 17,
face="bold",
margin=ggplot2::margin(r=10)),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(color = "white", size = 20),
axis.ticks.x = element_blank(),
panel.grid.major = element_line(colour = "grey90"),
panel.grid.minor = element_line(colour = "grey90"),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position = "top",
legend.key = element_rect(fill = "white"),
legend.text=element_text(size=15))
Data
b <- structure(list(corona = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C19", "Normal"
), class = "factor"), cons_week = c(185, 176, 190, 201, 184,
170, 202, 179, 203, 178, 206, 208, 209, 193, 181, 168, 191, 171,
192, 195, 186, 175, 187, 174, 207, 169, 205, 197, 200, 173, 204,
199, 189, 180, 194, 188, 182, 177, 196, 183, 124, 111, 75, 148,
27, 158, 1, 62, 11, 56, 57, 154, 141, 51, 112, 159, 116, 8, 126,
121, 38, 9, 78, 122, 32, 63, 94, 129, 76, 43, 44, 103, 84, 89,
92, 37, 67, 19, 73, 142), n = c(17L, 9L, 16L, 15L, 12L, 9L, 15L,
17L, 11L, 12L, 15L, 14L, 12L, 15L, 13L, 11L, 17L, 14L, 15L, 11L,
19L, 14L, 16L, 15L, 14L, 13L, 20L, 18L, 9L, 20L, 20L, 18L, 15L,
10L, 13L, 14L, 21L, 13L, 23L, 18L, 14L, 7L, 14L, 13L, 12L, 14L,
14L, 16L, 19L, 10L, 14L, 11L, 18L, 12L, 18L, 8L, 10L, 15L, 19L,
21L, 17L, 11L, 10L, 11L, 14L, 18L, 15L, 13L, 17L, 18L, 15L, 14L,
17L, 9L, 16L, 15L, 17L, 17L, 13L, 12L)), row.names = c(NA, -80L
), groups = structure(list(corona = structure(1:2, .Label = c("C19",
"Normal"), class = "factor"), .rows = structure(list(1:40, 41:80), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
#brunosm's answer is the way to solve this problem. On r2evans's suggestion, I offer an answer that is more illustrative on the reason why alpha doesn't seem to be working when used with geom_rect(). In truth, you can actually still use geom_rect(), although annotate() is the better option.
The answer lies in the inherent difference between geom_rect() and annotate(geom="rect".... which is that geoms draw things based on your data=, whereas annotate() does not.
Practically-speaking, it's easiest to see by demonstration. You've already posted that you get a solid box when you use the following code below. Please, note that p in this case is your entire plot code without the geom_rect() part:
p <- # your plot code entirely, but leave out geom_rect()
p + geom_rect(
aes(xmin = 1, xmax = 30, ymin = 0, ymax = 5),
color = "#2C77BF", fill = alpha("#2C77BF", .5))
That code gives you a solid blue rectangle and it looks like alpha did not work. In reality, this is not quite the case, and the setting for alpha worked just fine. Let's look at the following counterexample. I'll do the same plot, but this time pass only the first observation in the dataset b to geom_rect(). Note the different result:
p + geom_rect(data=b[1,],
aes(xmin = 1, xmax = 30, ymin = 0, ymax = 5),
color = "#2C77BF", fill = alpha("#2C77BF", .5))
It's working as expected! This is because since geom_rect() is a geom, it is basing its draw method on the source data, which is in this case b in its entirety. That means that for every observation in b, geom_rect() is drawing a rectangle with alpha 0.5. In the end, the dark rectangle you are getting is a result of overplotting - in the case of your dataset, it is the result of drawing 80 rectangles with alpha = 0.5 on top of one another.
For even more illustration, note what happens when I pass two of the observations in b to geom_rect():
p + geom_rect(data=b[1:2,],
aes(xmin = 1, xmax = 30, ymin = 0, ymax = 5),
color = "#2C77BF", fill = alpha("#2C77BF", .5))
Our rectangle is now a bit darker, since ggplot is drawing two rectangles in that same location.
Bottom line here is that annotate() takes the data= source out of the picture. It means you can't annotate something that changes with your data, but if you had that situation, you would use geom_rect().
I guess if you really want to do it this way with geom_rect(), there's no real problem to do it using geom_rect(data=b[1,],...... it's just probably not the right way to do it. :)
You are using the wrong function. Instead of geom_rect(), use annotate(), and put the alpha parameter inside. You also don't need aes().
(...) +
annotate(geom = "rect", xmin = 1, xmax = 30,
ymin = 0, ymax = 5,
color = "#2C77BF",
fill = "#2C77BF", alpha = 0.5) +
(...)

Order the x-axis in ggplot based on the exact order in my table

Given the data frame:
structure(list(`Number of patents` = structure(c(1L, 2L, 4L,
6L, 8L, 10L, 12L, 3L, 5L, 7L, 9L, 11L, 13L), .Label = c("0-9",
"10-19", "100-199", "20-29", "200-299", "30-39", "300-399", "40-49",
"400-499", "50-74", "500-999", "75-99", "Over 1000"), class = "factor"),
`Number of companies` = c(397, 102, 54, 30, 18, 35, 22, 39,
28, 13, 11, 34, 69)), .Names = c("Number of patents", "Number of companies"
), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"
))
When I use ggplot, it reorders my x-acis "Number of patents" numerically so instead of going "0-9", "10-19", "20-29", "30-39", it changes the order to:
"0-9", "10-19", "100-199", "20-29", etc. which of course makes no sense. Is there any way that I can force R to just keep the same ordering as the data frame that I try to plot from?
This is my ggplot:
ggplot(companiesoverpatents, aes(x= `Number of patents`, y= `Number of companies`)) +
geom_bar(stat="identity",position="dodge", fill = "#00AFBB") +
ggtitle("Companies over patents") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x=element_text(angle = 45, vjust = 0.5))
Well, it makes some sense because number of patents is ordered alphabetically. To achieve what you want you need to change the order of levels in factor:
companiesoverpatents$`Number of patents` <- factor(companiesoverpatents$`Number of patents`, levels = unique(companiesoverpatents$`Number of patents`))
As a side note, try to avoid spaces in column names.
You need to re-level the factors in your dataset:
companiesoverpatents[,1]<-factor(unlist(companiesoverpatents[,1]),levels=unlist(companiesoverpatents[,1]))

Reverse order of x axis labels on grouped bar chart after coord_flip()

In the plot shown below I am trying to change the order of the labels on the y axis (x axis before coord_flip()).
I would like 1 to be on top and 16 at the bottom.
levels <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
library(ggplot2)
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group), group=qid) +
geom_bar(stat="identity", position=position_dodge()) +
scale_x_discrete(name = "Questions", limits = levels) +
scale_y_continuous(name = "Percent correct") +
coord_flip()
Here is what I have tried to so far:
levels <- c(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) -> No change.
limits = rev(levels) -> no change
limits = rev(levels(levels)) -> suggestion from this question/answer img below
Reproducible example with a subset of the questions (8,9,10,11)
dput() output:
structure(list(group = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), correct_per = c(90.4761904761905, 100, 100, 87.5, 83.3333333333333, 90.9090909090909, 84.6153846153846, 87.5, 80.9523809523809, 88.6363636363636, 100, 70.8333333333333, 63.4146341463415, 76.7441860465116, 76.9230769230769, 62.5), nr_correct = c(38L, 44L, 26L, 21L, 35L, 40L, 22L, 21L, 34L, 39L, 26L, 17L, 26L, 33L, 20L, 15L), nr_incorrect = c(4L, 0L, 0L, 3L, 7L, 4L, 4L, 3L, 8L, 5L, 0L, 7L, 15L, 10L, 6L, 9L), length = c(42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 41L, 43L, 26L, 24L), qid = c("8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11")), .Names = c("group", "correct_per", "nr_correct", "nr_incorrect", "length", "qid"), row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame"))
save to file
all_Q <- dget(filename)
levels <- c(8,9,10,11)
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group), group=qid) +
geom_bar(stat="identity", position=position_dodge()) +
scale_x_discrete(name = "Questions", limits = levels) +
scale_y_continuous(name = "Percent correct") +
coord_flip()
Column all_Q$qid is factor, thus you can specify limits (order) passing non-numeric vector.
library(ggplot2)
# Don't use group as you're already grouping by fill
ggplot(all_Q, aes(qid, correct_per, fill = group)) +
geom_bar(stat = "identity", position = "dodge") +
# Passing character vector from 16 to 1
scale_x_discrete(limits = as.character(16:1)) +
# Don't use function scale_y_* only for naming
# With function labs you can specify all the names
labs(x = "Questions",
y = "Correct, %",
fill = "My fill") +
coord_flip()
It's best to work with factors in specific order. I'm sure what you want is
all_Q$qid <- factor(all_Q$qid, levels = c(11, 10, 9, 8))
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group), group=qid) +
geom_bar(stat="identity", position=position_dodge()) +
scale_x_discrete(name = "Questions") +
scale_y_continuous(name = "Percent correct") +
coord_flip()
Yields:
Compare:
all_Q$qid <- factor(all_Q$qid, levels = c(11, 10, 9, 8))
Yields:

Setting range and breaks on scale on ggplot2

Using a sample dataframe:
df <- structure(list(SITCD = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("GSO/TO", "IKOF", "JL",
"MES", "SSD", "USSD"), class = "factor"), Code = structure(c(27L,
21L, 3L, 25L, 26L, 20L, 2L, 28L, 230L, 16L, 4L, 10L, 15L, 1L), .Label = c("AAR-2107",
"AAR-643", "AAR-644", "AAR-995", "HAR-2956", "HAR-2957", "I-430",
"I-431", "I-432", "I-9490", "I-9491", "K-1461", "K-1740", "K-1915",
"K-2034", "K-2096", "K-2385", "K-2386", "K-2387", "K-3112", "K-3220",
"K-3224", "Lu-1095", "Lu-1103", "LU-3282", "LU-3283", "LU-3284",
"LU-3400", "Lu-487", "Lu-489,90", "Lu-491,92", "Lu-528", "Lu-529",
"Lu-530", "Lu-531", "Lu-585", "Lu-586", "Lu-608", "Lu-646", "Lu-647",
"Lu-648", "Lu-711", "Lu-714", "Lu-766", "Lu-768", "Lu-790", "Lu-792",
"Lu-793", "Lu-826", "Lu-827", "Lu-828", "Lu-829", "Lu-830", "Lu-831",
"Lu584", "M-1611", "M-1612", "M-1613", "M-1614", "M-1615", "M-1616",
"M-1617", "M-1618", "M-1619", "M-1620", "M-1621", "M-1622", "M-1623",
"M-1624", "OS-49305", "OS-49306", "OS-49308", "OS-49309", "OS-49311",
"OS-49312", "OS-49313", "OS-49314", "OS-49315", "OS-49384", "OS-49385",
"OS-49386", "OS-49387", "OS-49403", "OS-49414", "OS-49437", "OS-49440",
"OS-49441", "OS-49442", "OS-49493", "OS-49496", "OS-49499", "OS-49502",
"OS-49506", "OS-49515", "OS-49516", "OS-49517", "OS-49518", "OS-49519",
"OS-49520", "OS-49555", "OS-49558", "OS-49562", "OS-49565", "OS-49578",
"OS-49580", "OS-49581", "OS-49582", "OS-49583", "OS-49584", "OS-49605",
"OS-49606", "OS-49607", "OS-51568", "OS-51716", "OS-51759", "OS-51760",
"OS-51765", "OS-51766", "OS-51767", "OS-51769", "OS-51770", "OS-51774",
"OS-51775", "OS-51776", "OS-51845", "OS-51846", "OS-51847", "OS-51874",
"OS-51875", "OS-51882", "OS-51883", "OS-51884", "OS-51885", "OS-52112",
"OS-52956", "OS-52957", "OS-52962", "OS-52963", "OS-52964", "OS-52966",
"OS-52967", "OS-52968", "OS-52969", "OS-52970", "OS-54002", "OS-54004",
"OS-54005", "OS-54006", "OS-54007", "OS-54008", "OS-54009", "OS-54045",
"OS-54046", "OS-54048", "OS-54073", "OS-54074", "OS-54075", "OS-54076",
"OS-54077", "OS-54892", "OS-55609", "OS-55610", "OS-55611", "OS-55612",
"OS-55613", "OS-55614", "OS-55724", "OS-55725", "OS-55728", "OS-55729",
"OS-55730", "OS-55731", "OS-55732", "OS-55733", "OS-55734", "OS-55735",
"OS-55736", "OS-55737", "OS-58249", "OS-58250", "OS-58324", "OS-58325",
"OS-58326", "OS-58327", "OS-58509", "OS-58606", "OS-58607", "OS-58609",
"OS-58673", "OS-58674", "OS-58701", "OS-58702", "OS-58703", "OS-58704",
"OS-58705", "OS-58732", "OS-58735", "OS-59579", "OS-62849", "OS-62850",
"OS-62851", "OS-62852", "OS-62855", "OS-62985", "OS-62986", "OS-62992",
"OS-62994", "OS-64754", "OS-64755", "OS-64756", "OS-64759", "OS-64760",
"OS-64762", "OS-64764", "OS-64765", "OS-64766", "OS-64843", "OS-64844",
"OS-64845", "OS-64849", "OS-65398", "OS-65399", "OS-65401", "OS-65405",
"OS-65406", "OS-65435", "OS-65436", "OS-65437", "OS-65438", "T-10382",
"Unknown", "W-1381", "Y596", "Y599", "Y600", "Y602", "Y702",
"Y703", "Y704", "Y708", "Y711", "Y712", "Y713", "Y714", "Y716",
"Y717", "Y876", "Y878", "Y879", "Y882", "Y883", "Y884"), class = "factor"),
Type = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 1L, 1L), .Label = c("Above", "At", "Below"), class = "factor"),
RSL = c(5, 8, 17.5, 19, 27, 30, 30, 33, 35, 40, 40, 50, 53,
70), RSL_error = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5,
2), Age = c(8183.5, 9221.5, 10424.5, 10069, 9092, 10465.5,
9204.5, 10531.5, 9844.5, 10073.5, 9905, 9907.5, 11660, 10698.5
), age_error = c(232.5, 295.5, 519.5, 371, 323, 377.5, 336.5,
324.5, 318.5, 408.5, 327, 380.5, 463, 394.5), x_min_error = c(7951L,
8926L, 9905L, 9698L, 8769L, 10088L, 8868L, 10207L, 9526L,
9665L, 9578L, 9527L, 11197L, 10304L), x_max_error = c(8416L,
9517L, 10944L, 10440L, 9415L, 10843L, 9541L, 10856L, 10163L,
10482L, 10232L, 10288L, 12123L, 11093L), y_min_error = c(3,
6, 15.5, 17, 25, 28, 28, 31, 33, 38, 38, 48, 48, 68), y_max_error = c(7,
10, 19.5, 21, 29, 32, 32, 35, 37, 42, 42, 52, 58, 72)), .Names = c("SITCD",
"Code", "Type", "RSL", "RSL_error", "Age", "age_error", "x_min_error",
"x_max_error", "y_min_error", "y_max_error"), row.names = c(NA,
14L), class = "data.frame")
I wish to draw a graph using the following code:
g <- ggplot (df, aes(x=Age, y=RSL, shape = Type)) +
geom_point() +
scale_shape_manual(values=c(1,15,5)) + #makes open circle/triangle
theme(axis.line=element_line(colour = "black", size = 0.5, linetype = "solid")) + # adds solid black x and y axis
geom_errorbar(aes(ymin=y_min_error, ymax=y_max_error,width=0,)) + # y error bar
geom_errorbarh(aes(xmin=x_min_error, xmax=x_max_error,height=0,)) +
theme_classic() +
theme_bw()+ #Black outline around the graph
xlim(0, 14000) +#Set axis limits
ylim(0, 120) +
#scale_x_continuous(breaks=seq(0,14000,2000))+
#scale_y_continuous(breaks=seq(0,120,20))+
theme(legend.position="bottom")
g
I was wondering why I am having difficulty setting the axes scale. I am trying to use the scale_x_continuous(breaks=seq(...) code which wasn't working. I then read elsewhere that I had to set the limits of the scales which I did with xlim/ylim but I can't use this with the scale_x_continuous code as I get the error message:
Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.
Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.
Does anyone have any ideas?
Replace xlim(0, 14000) with scale_x_continuous(breaks=seq(1, 15000, 1000), limits = c(0, 14000))
Tidier code:
library(ggplot2)
ggplot(df, aes(Age, RSL, shape = Type)) +
geom_point() +
geom_errorbarh(aes(xmin = x_min_error,
xmax = x_max_error,
height = 0)) +
geom_errorbar(aes(ymin = y_min_error,
ymax = y_max_error,
width = 0)) +
scale_shape_manual(values = c(1, 15, 5)) +
scale_y_continuous(limits = c(0, 120)) +
scale_x_continuous(breaks=seq(1, 15000, 1000),
limits = c(0, 14000))

Resources