본문 바로가기
반응형

6. 데이터 시각화 - ggplot2/공통11

[ggplot2] x축과 y축 제목,눈금,라벨 제거 1. 축 제목 제거 x축 제목 제거 +theme(axis.title.x = element_blank()) y축 제목 제거 +theme(axis.title.y = element_blank()) 2. 축 눈금 제거 x축 눈금 제거 +theme(axis.ticks.x=element_blank()) y축 눈금 제거 +theme(axis.ticks.y=element_blank()) 3. 축 눈금 라벨 제거 x축 눈금 라벨 제거 +theme(axis.text.x=element_blank()) y축 눈금 라벨 제거 +theme(axis.text.x=element_blank()) 2023. 4. 6.
[ggplot2] x,y 변수명 문자로 입력하는 법 aes 대신 aes_string 을 사용하면 됩니다. 데이터프레임이 md 이고, 변수가 group, var 이라면 아래와 같이 입력합니다. ggplot(data=md,aes_string(x="group",y="var")) 2023. 1. 18.
[ggplot2] 화면분할하는 방법 tidyverse 외에 gridExtra 라는 패키지가 필요합니다. 패키지를 설치합니다. intall.packages('tidyverse') intall.packages('gridExtra') 패키지를 불러옵니다. library(tidyverse) library(ggplot2) 화면분할은 grid.arrange 라는 함수를 사용합니다. 각 그래프를 변수에 입력한 뒤, 변수를 grid.arrange 함수에 넣어줍니다. ncol, nrow 옵션을 이용해서 화면을 어떻게 분할할지 결정합니다. 예시는 아래와 같습니다. library(tidyverse) library(gridExtra) #데이터프레임 생성 x1=c(1,1,2,2) y1=c(1,2,1,2) df=data.frame(x1,y1) #각 그래프를 변수에.. 2023. 1. 18.
[ggplot2] 화면 분할 그래프 for문 이용해서 그리기 ggplot2 와 gridExtra 패키지가 사용됩니다. 설치가 안되신 분들은 먼저 설치해주셔야 합니다. 화면 분할하는 방법을 먼저 읽고 오셔야 이해가 쉽습니다. [ggplot2] 화면분할하는 방법 (tistory.com) 화면 분할 그래프를 for문을 이용해서 그려봅시다. 원리는 간단합니다. 리스트를 생성한 뒤, for문을 이용하여 ggplot 그래프를 list 의 각 원소에 입력합니다. do.call 함수로 grid.arrange 함수를 리스트의 모든 원소에 적용해줍니다. grid.arrange 함수는 ggplot 그래프를 변수로 입력받아 화면을 분할하여 그래프를 그려주는 함수입니다. 예시는 아래와 같습니다. 설명은 주석으로 대신합니다. library(gridExtra) library(ggplot2).. 2023. 1. 18.
[R ggplot] 범례 추가하고 이름,크기,색,진하기 조절하기 데이터는 내장데이터 iris 를 사용하였습니다. 설명은 주석으로 대신합니다. library(ggplot2) ggplot()+ geom_point(data=iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+ #범례 추가 theme(legend.position = 'top')+ #범례 타이틀 색,크기,진하게 설정 theme(legend.title = element_text(color = "black", size = 20, face = "bold"))+ #범레 텍스트 색,크기,진하게 설정 theme(legend.text = element_text(color = "black", size = 12, face = "bold"))+ #범례 색 설정 scale_color.. 2022. 6. 8.
[R ggplot2] 축 눈금 레이블 크기, bold 설정 (x축, y축) x축 기준으로 설명하겠습니다. 축 눈금 레이블의 크기, bold를 설정할 때는 theme 함수 안에 axis.text.x 옵션을 사용합니다. 아래와 같이 사용합니다. ggplot()+ geom_boxplot(data=iris, aes(x=Species,y=Sepal.Length))+ theme(axis.text.x = element_text(size=20,face='bold')) 2022. 5. 31.
[R ggplot2] 축 제목, 크기, bold 설정 (x축, y축) x축 기준으로 설명하겠습니다. 축 제목을 설정할 때는 xlab 함수를 사용합니다. 축 제목 크기와 bold 등을 설정할 때는 theme 함수 안에 axis.title.x 옵션을 사용합니다. 아래와 같이 사용합니다. ggplot()+ geom_boxplot(data=iris, aes(x=Species,y=Sepal.Length))+ xlab("종") + #x축 이름 theme(axis.title.x = element_text(size=20,face='bold')) #축 제목 크기, bold 설정 2022. 5. 31.
[R ggplot2] x축 라벨 각도 회전하기 x축 라벨 각도를 회전할 때는 theme 함수를 사용합니다. 아래와 같이 입력해줍니다. theme(axis.text.x=element_text(angle=90, hjust=1)) angle 에 원하는 각도를 넣으면 됩니다. 아래는 예시입니다. library(ggplot2) ggplot()+ geom_bar(data=mtcars,aes(x=row.names(mtcars),y=hp),stat='identity')+ theme(axis.text.x=element_text(angle=90, hjust=1)) 2022. 5. 31.
[R ggplot2] x축 범위, y축 범위 설정 방법 x축 범위 설정에는 scale_x_continuous 함수를, y축 범위 설정에는 scale_x_continuous 함수를 사용합니다. 예시는 아래와 같습니다. ggplot()+ geom_point(data=iris, aes(x=Sepal.Length,y=Sepal.Width))+ scale_x_continuous(limits = c(0, 100))+ scale_y_continuous(limits = c(0, 100)) 2022. 5. 30.
[R ggplot2] 그래프 제목 추가, 크기변경, 두껍게 하는 법 그래프를 하나 그려봅시다. 정규분포 그래프를 그렸습니다. x1=seq(-4,4,0.1) y1=dnorm(x1) data1=data.frame(x1,y1) library(ggplot2) ggplot()+ geom_point(data=data1, aes(x=x1,y=y1)) 그래프 제목을 추가할 때는 ggtitle 함수를 사용합니다. x1=seq(-4,4,0.1) y1=dnorm(x1) data1=data.frame(x1,y1) library(ggplot2) ggplot()+ geom_point(data=data1, aes(x=x1,y=y1))+ ggtitle('Normal Distribution') 가운데 정렬, 크기,bold 는 아래와 같이 설정합니다. hjust=0.5 로 설정하면 가운데 정렬 됩니다... 2022. 5. 13.
[R ggplot2] 히스토그램과 선그래프 겹쳐그리기 히스토그램과 선 그래프를 겹쳐그리는 방법입니다. ggplot 을 생성하고, geom_histrogrm 으로 히스토그램을 그립니다. 이어서 geom_line 으로 선 그래프를 그려줍니다. 아래는 코드입니다. 주석에 설명을 적어놓았습니다. library(ggplot2) #1. 데이터 생성 df1=as.data.frame(islands) #히스토그램데이터 df2=data.frame(x2=0:15000,y2=dnorm(0:15000,mean=5000,sd=200)) #선그래프 데이터 #2. 그래프 그리기 ggplot(df1,aes(x=islands))+ geom_histogram(aes(y=..density..))+ #히스토그램 geom_line(data=df2,aes(x=x2,y=y2),color='red').. 2022. 5. 12.
반응형