簡體   English   中英

改變x軸label R

[英]change x-axis label R

我開始使用 R,並第一次嘗試 plot 數據,我將數據集變成了 data.table,看起來像這樣

在此處輸入圖像描述

現在我正在嘗試 plot 它,在 Y 軸上進行攻擊,我想要 X 軸上的類型,但我似乎無法讓它工作。 我得到的最接近的看起來像這樣在此處輸入圖像描述

並用plot(best$Ataque_Combinado,xlab=best$type,pch=5,cex.lab=0.7)

data.table 包含使用 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>)```

我已經嘗試過axis() mtext() cex.lab xaxt ,但這些似乎沒有幫助,或者沒有正確使用,在此先感謝您的幫助

鑒於您沒有正確提供data frame ,您可以執行以下操作,我使用了一個虛擬示例:

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)

或者在您的情況下, typestring

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)

結果是:

在此處輸入圖像描述

因此,將type列轉換為具有所需標簽的因子將為您提供所需的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM