r - Setting range and breaks on scale on ggplot2 -
using 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 draw graph using 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 , 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 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 wondering why having difficulty setting axes scale. trying use scale_x_continuous(breaks=seq(...) code wasn't working. read elsewhere had set limits of scales did xlim/ylim can't use scale_x_continuous code error message:
scale 'x' present. adding scale 'x', replace existing scale. scale 'y' present. adding scale 'y', replace existing scale. does have ideas?
replace xlim(0, 14000) 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))
Comments
Post a Comment