最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - How to tweak specifics on barchart - Stack Overflow

programmeradmin7浏览0评论

I need some help making my barcharts look similar to this one in terms of the color and background

These are the charts I currently have

I'd like to get rid of the gray bocks in the background, add lines on the x/y axes, change the color of the fill, and order the months correctly (May, June, July, Aug, Sept).

This is the code I used to create my barchart:

library(ggplot2)
data<- data.frame(
Month = c('MAY', 'MAY', 'MAY', 'MAY', 'JUNE', 'JUNE', 'JUNE', 'JUNE','JULY','JULY','JULY','JULY','AUG','AUG','AUG','AUG','SEPT','SEPT','SEPT','SEPT'),
Class = c('Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida'),
Avg = c(3.1667, 5.3333, 4.4000, 36.1667, 12.0000, 1.6667, 31.2857, 42.7142,3.4000,0.0000,1.0000,73.6667,1.4286,1.5000,2.0000,68.3333,4.2727,0.0000,5.2000,62.5000))
data
ggplot(data=data,aes(x=Month,y=Avg,fill=Class))+
geom_bar(stat="identity")+
ylab("Average Number / Ponar Sample")

If you can help I'd greatly appreciate it. Thanks!

I need some help making my barcharts look similar to this one in terms of the color and background

These are the charts I currently have

I'd like to get rid of the gray bocks in the background, add lines on the x/y axes, change the color of the fill, and order the months correctly (May, June, July, Aug, Sept).

This is the code I used to create my barchart:

library(ggplot2)
data<- data.frame(
Month = c('MAY', 'MAY', 'MAY', 'MAY', 'JUNE', 'JUNE', 'JUNE', 'JUNE','JULY','JULY','JULY','JULY','AUG','AUG','AUG','AUG','SEPT','SEPT','SEPT','SEPT'),
Class = c('Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida','Insecta', 'Bivalvia','Crustacea','Annelida'),
Avg = c(3.1667, 5.3333, 4.4000, 36.1667, 12.0000, 1.6667, 31.2857, 42.7142,3.4000,0.0000,1.0000,73.6667,1.4286,1.5000,2.0000,68.3333,4.2727,0.0000,5.2000,62.5000))
data
ggplot(data=data,aes(x=Month,y=Avg,fill=Class))+
geom_bar(stat="identity")+
ylab("Average Number / Ponar Sample")

If you can help I'd greatly appreciate it. Thanks!

Share Improve this question edited Mar 20 at 18:58 M-- 29.5k10 gold badges69 silver badges106 bronze badges Recognized by R Language Collective asked Mar 20 at 18:02 Dylan RaneyDylan Raney 173 bronze badges 3
  • 2 "gray blocks in the background", you can add either theme_bw() or theme_minimal() to your ggplot chain; "add lines to the x/y axes", look at scale_x_continuous (and _y_), specifically its breaks= and labels= args – r2evans Commented Mar 20 at 18:09
  • 1 as for order of dates, realize that R doesn't see ordinal months, it sees strings, and the default will always be to order the strings alphabetically; to change that behavior, change Month to a factor and specify its levels=, which ggplot will honor as the correct order. – r2evans Commented Mar 20 at 18:11
  • 3 these questions are rather fundamental to ggplot's functionality, I suggest that browsing one of its many tutorials (starting with ggplot2.tidyverse.) will be very fruitful for you; it has a small learning curve if you're coming from base R graphics, but it's worth it to read through the docs. For hasty research, I often see r-graph-gallery as a good recommendation, it has a "picture menu" experience: find the pic you want to emulate, and it shows you the data and code it's using. – r2evans Commented Mar 20 at 18:14
Add a comment  | 

2 Answers 2

Reset to default 3
cols <- c("Crustacea" = "#75140c","Bivalvia" = "#ef8633",
           "Insecta" = "#C0be3d", "Annelida" = "#377d22")

ggplot(data = data, aes(x = factor(Month, 
                                   levels = c("JAN", "FEB", "MAR",
                                              "APR", "MAY", "JUNE", 
                                              "JULY", "AUG", "SEPT",
                                              "OCT", "NOV", "DEC")), 
                        y = Avg, 
                        fill = factor(Class,
                                      levels = c("Annelida", "Insecta",
                                                 "Bivalvia","Crustacea")))) +
  geom_bar(stat = "identity", color = "black", linewidth = 0.1) +
  ylab("Average Number / Ponar Sample") +
  xlab("Month") +
  scale_y_continuous(limits = c(0, 110),
                     breaks = (0:4) * 20) +
  scale_fill_manual(values = cols, 
                    breaks=c('Crustacea', 'Bivalvia', 'Insecta', 'Annelida')) +
  theme_classic() +
  theme(legend.key.spacing.y = unit(0.15, 'cm'),
        legend.key.height = unit(0.2, 'cm'),
        legend.key.width = unit(0.9, 'cm'),
        legend.position=c(0.85,.85),
        legend.title = element_blank())

Should be pretty close. Not always worth making every last tweak if not necessary. Includes some extra spacing on the x-axis, some spacing for text, placement of legend, removal of legend name, colors, removal of expansion below bars, etc. theme_classic() is a good place to start for the basic X vs. Y lines simple plot.

library(ggplot2)

data <- data.frame(
  Site = c("GC7", "GC9", "GC7", "GC9", "GC7", "GC9", "GC7", "GC9"),
  Class = c("Insecta", "Insecta", "Bivalvia", "Bivalvia", "Crustacea", "Crustacea", "Annelida", "Annelida"),
  Avg = c(4.1111, 2.5833, 1.0000, 3.2857, 2.7500, 14.6667, 31.6880, 82.4000)
)

data$Class <- factor(data$Class, levels = c(
  "Annelida", "Insecta", "Bivalvia", "Crustacea"))

p1 <- ggplot(data, aes(Site, Avg)) +
  geom_col(aes(fill = Class), width = 0.7, color = "grey30") +
  scale_fill_manual(values = c(
    "Crustacea" = "#75150C", "Bivalvia" = "#EF8634",
    "Insecta" = "#C0BE3D", "Annelida" = "#387D21"
  )) +
  scale_x_discrete(expand = expansion(add = c(1, 1))) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  theme_classic(18) +
  theme(
    axis.line = element_line(color = "grey50"),
    axis.text.y = element_text(margin = margin(r = 10)),
    axis.text.x = element_text(margin = margin(t = 10)),
    axis.ticks.y = element_line(color = "grey50"),
    axis.ticks.x = element_line(color = "grey50"),
    axis.ticks.length = unit(7, "pt"),
    axis.title.y = element_text(margin = margin(r = 15)),
    legend.position = "inside",
    legend.position.inside = c(0.25, 0.7),
    legend.key.width = unit(30, "pt"),
    legend.key.height = unit(15, "pt"),
    legend.key.spacing.y = unit(5, "pt")
  ) +
  guides(fill = guide_legend(reverse = TRUE)) +
  labs(
    x = NULL,
    y = "Average Number / Ponar Sample",
    fill = NULL
  )

data2 <- data.frame(
  Month = c("MAY", "MAY", "MAY", "MAY", "JUNE", "JUNE", "JUNE", "JUNE", "JULY", "JULY", "JULY", "JULY", "AUG", "AUG", "AUG", "AUG", "SEPT", "SEPT", "SEPT", "SEPT"),
  Class = c("Insecta", "Bivalvia", "Crustacea", "Annelida", "Insecta", "Bivalvia", "Crustacea", "Annelida", "Insecta", "Bivalvia", "Crustacea", "Annelida", "Insecta", "Bivalvia", "Crustacea", "Annelida", "Insecta", "Bivalvia", "Crustacea", "Annelida"),
  Avg = c(3.1667, 5.3333, 4.4000, 36.1667, 12.0000, 1.6667, 31.2857, 42.7142, 3.4000, 0.0000, 1.0000, 73.6667, 1.4286, 1.5000, 2.0000, 68.3333, 4.2727, 0.0000, 5.2000, 62.5000)
)
data2$Month <- factor(data2$Month, levels = c(
  "MAY", "JUNE", "JULY", "AUG", "SEPT"))
data2$Class <- factor(data2$Class, levels = c(
  "Annelida", "Insecta", "Bivalvia", "Crustacea"))


p2 <- ggplot(data2, aes(Month, Avg)) +
  geom_col(aes(fill = Class), width = 0.7, color = "grey30") +
  scale_fill_manual(values = c(
    "Crustacea" = "#75150C", "Bivalvia" = "#EF8634",
    "Insecta" = "#C0BE3D", "Annelida" = "#387D21"
  )) +
  scale_x_discrete(expand = expansion(add = c(1, 1))) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  theme_classic(18) +
  theme(
    axis.line = element_line(color = "grey50"),
    axis.text.y = element_text(margin = margin(r = 10)),
    axis.text.x = element_text(margin = margin(t = 10)),
    axis.ticks.y = element_line(color = "grey50"),
    axis.ticks.x = element_line(color = "grey50"),
    axis.ticks.length = unit(7, "pt"),
    axis.title.y = element_text(margin = margin(r = 15)),
    legend.position = "right",
    legend.key.width = unit(30, "pt"),
    legend.key.height = unit(15, "pt"),
    legend.key.spacing.y = unit(5, "pt")
  ) +
  guides(fill = guide_legend(reverse = TRUE)) +
  labs(
    x = NULL,
    y = "Average Number / Ponar Sample",
    fill = NULL
  )

p <- cowplot::plot_grid(p1, p2, nrow = 1, rel_widths = c(1, 1.5))
png(width = 14, height = 6, units = "in", res = 300); p; dev.off()

发布评论

评论列表(0)

  1. 暂无评论