简体   繁体   中英

change x-axis label R

im getting started on R, and doing my first attempts to plot data, i turned a dataset into a data.table that looks like this

在此处输入图像描述

and now im trying to plot it, having the attack on the Y axis, and i want the type on the X axis, but i cant seem to get it working. the closest i've gotten looks like this在此处输入图像描述

and was plotted with plot(best$Ataque_Combinado,xlab=best$type,pch=5,cex.lab=0.7)

the data.table contains the following info retrieved with dput():

structure(list(type = c("dragon flying", "psychic", "ground", 
"psychic ghost", "water", "rock fairy", "steel ghost", "water dark", 
"dragon ground", "fire fighting", "dragon psychic", "dragon ice", 
"fighting steel", "bug fighting", "dragon electric", "water dragon", 
"steel dragon", "dragon fire", "dark", "fire ground", "grass ice", 
"fairy", "dark flying", "fire flying", "rock dark", "electric", 
"ghost", "normal", "grass", "rock flying", "ground flying", "steel psychic", 
"psychic steel", "electric flying", "psychic fairy", "water ground", 
"grass steel", "bug steel", "ice", "ghost dragon", "fire water", 
"ghost poison", "rock", "psychic fighting", "grass dark", "grass psychic", 
"dark dragon", "dark fire", "water flying", "fire", "steel fairy", 
"grass grass", "bug", "grass poison", "fighting", "fire steel", 
"ghost grass", "grass fairy", "normal flying", "bug electric", 
"electric fairy", "dragon fighting", "dragon", "flying", "steel flying", 
"grass ghost", "normal psychic", "water psychic", "rock fighting", 
"steel rock", "ice ground", "poison", "psychic grass", "psychic fire", 
"water fighting", "water fairy", "electric steel", "ghost fire", 
"ground rock", "water steel", "rock bug", "fire dark", "fighting ice", 
"fighting dark", "bug fire", "normal dragon", "poison fighting", 
"grass fighting", "water rock", "rock dragon", "bug flying", 
"poison ground", "ground steel", "bug water", "dark steel", "grass ground", 
"ghost dark", "ground dark", "fire psychic", "steel ground", 
"normal fighting", "rock water", "ground ghost", "fighting psychic", 
"ground dragon", "water ice", "psychic flying", "steel fighting", 
"electric electric", "ice flying", "rock poison", "rock ground", 
"fire normal", "rock ice", "ice water", "ghost ground", "poison fire", 
"bug grass", "poison dragon", "poison poison", "bug rock", "steel", 
"dark ghost", "ghost flying", "electric ghost", "fire dragon", 
"fairy flying", "bug poison", "fire fire", "dark ice", "fighting flying", 
"flying dragon", "poison dark", "ice psychic", "electric normal", 
"rock grass", "normal grass", "dark psychic", "poison flying", 
"ice ghost", "water grass", "normal fairy", "ground ground", 
"water poison", "rock psychic", "bug fairy", "fire ice", "ground electric", 
"grass flying", "water ghost", "ghost fairy", "normal water", 
"ground psychic", "fire rock", "dark fighting", "normal dark", 
"water electric", "ground fire", "rock steel", "ground ice", 
"bug ghost", "water bug", "poison water", "normal ground", "poison bug", 
"bug ground"), Ataque_Combinado = c(1.82223460573976, 1.73223739203121, 
1.66759543048203, 1.66258010587908, 1.6600724435776, 1.6110337141265, 
1.50543326831987, 1.49387015881861, 1.45890220117024, 1.45639453886877, 
1.44887155196434, 1.44636388966286, 1.42685984954026, 1.36815268877125, 
1.35079409306213, 1.3432711061577, 1.3432711061577, 1.3432711061577, 
1.32502089718585, 1.31749791028142, 1.31535246586793, 1.30479242128727, 
1.30479242128727, 1.30317637224854, 1.29760378935637, 1.29426023962106, 
1.29269991641126, 1.27598216773475, 1.26344385622736, 1.25550292560602, 
1.24644747840624, 1.24644747840624, 1.24444134856506, 1.23641682920033, 
1.23140150459738, 1.22192811368069, 1.20392867093898, 1.18863193089997, 
1.18863193089997, 1.18863193089997, 1.1861242685985, 1.14906659236556, 
1.14837001950404, 1.14837001950404, 1.13583170799666, 1.13332404569518, 
1.13332404569518, 1.12956255224296, 1.12008916132627, 1.11382000557258, 
1.10504318751741, 1.09674003900808, 1.09431596544999, 1.09083310114238, 
1.08679297854556, 1.07801616049039, 1.07283365840067, 1.06227361382001, 
1.04973530231262, 1.04722764001115, 1.0327389244915, 1.03148509334076, 
1.02897743103929, 1.02897743103929, 1.01891891891892, 1.01526887712455, 
1.01022568960713, 0.996935079409306, 0.98985789913625, 0.987461688492616, 
0.98495402619114, 0.977431039286709, 0.977431039286709, 0.977431039286709, 
0.975564224017832, 0.97091111730287, 0.969908052382279, 0.966146558930064, 
0.961688492616328, 0.958456394538869, 0.957926999164113, 0.955419336862636, 
0.954527723599889, 0.947366954583449, 0.941627194204514, 0.941627194204514, 
0.937698523265533, 0.933407634438562, 0.933045416550571, 0.931150738367233, 
0.930175536361103, 0.910922262468654, 0.908888269713012, 0.906380607411535, 
0.906380607411535, 0.897213708553915, 0.892950682641404, 0.888910560044581, 
0.88202842017275, 0.880607411535247, 0.880607411535247, 0.87809974923377, 
0.875202006129841, 0.874338255781555, 0.874338255781555, 0.873084424630816, 
0.871830593480078, 0.871830593480078, 0.87057676232934, 0.87057676232934, 
0.862552242964614, 0.853580384508219, 0.850849818891056, 0.847952075787127, 
0.843549735302313, 0.842295904151574, 0.83953747561995, 0.839008080245194, 
0.826831986625801, 0.824045695179716, 0.824045695179716, 0.822791864028977, 
0.819030370576762, 0.817776539426024, 0.814015045973809, 0.812120367790471, 
0.810253552521594, 0.809556979660072, 0.80523822791864, 0.802033992755642, 
0.800167177486765, 0.799804959598774, 0.790108665366397, 0.784480356645305, 
0.780579548620786, 0.77679019225411, 0.7712454722764, 0.769239342435219, 
0.768737809974923, 0.766230147673447, 0.76372248537197, 0.737949289495681, 
0.719699080523823, 0.718445249373084, 0.718445249373084, 0.708414600167177, 
0.701114516578434, 0.695709111173029, 0.687656728893842, 0.683895235441627, 
0.665645026469769, 0.66439119531903, 0.660629701866815, 0.655614377263862, 
0.63987183059348, 0.63234884368905, 0.626692672053497, 0.611590972415715, 
0.605321816662023, 0.59083310114238, 0.562552242964614, 0.557536918361661, 
0.555029256060184, 0.481861242685985, 0.346336026748398, 0.319308999721371
)), row.names = c(NA, -166L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x000001bf49fe1ef0>)```

i've tried axis() mtext() cex.lab xaxt but none od those seem to help, or havent been used correctly, thanks in advance for any help

You could do the following, given the fact you didnt provide the data frame properly I used a dummy example:

dat = data.frame(
  label = sample(c(1, 2, 3), 150, replace = TRUE),
  val = rgamma(150, 50)
)

dat$label = factor(
  dat$label, levels = 1:3,
  labels = c("First", "Second", "Third")
)

plot(dat$label, dat$val)

Or in your case the type is string :

dat = data.frame(
  label = sample(c("First", "Second", "Third"), 150, replace = TRUE),
  val = rgamma(150, 50)
)

dat$label = factor(
  dat$label)

plot(dat$label, dat$val)

The result is:

在此处输入图像描述

So turning the type column to factor with the desired labels will give you the result you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM