在ggplot2上设置范围和缩小比例

时间:2017-08-15 14:00:51

标签: r ggplot2

使用示例数据框:

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

我希望使用以下代码绘制图表:

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

我想知道为什么我在设置轴刻度时遇到困难。我正在尝试使用scale_x_continuous(breaks = seq(...)代码,这些代码无法正常工作。然后我在别处读到我必须设置我用xlim / ylim做的刻度限制,但我不能使用这与scale_x_continuous代码一样,因为我收到错误消息:

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.

有没有人有任何想法?

1 个答案:

答案 0 :(得分:3)

xlim(0, 14000)替换为scale_x_continuous(breaks=seq(1, 15000, 1000), limits = c(0, 14000))

更整洁的代码:

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