Ggplot2 का उपयोग करके r में गैंट चार्ट कैसे बनाएं
गैंट चार्ट एक प्रकार का चार्ट है जो विभिन्न घटनाओं के प्रारंभ और समाप्ति समय को दर्शाता है।
यह ट्यूटोरियल बताता है कि ggplot2 पैकेज का उपयोग करके R में गैंट चार्ट कैसे बनाया जाए।
Ggplot2 का उपयोग करके R में गैंट चार्ट बनाना
मान लें कि हमारे पास निम्नलिखित डेटा सेट है जो एक स्टोर में चार अलग-अलग श्रमिकों की शिफ्ट के प्रारंभ और समाप्ति समय को दर्शाता है:
#create data frame
data <- data.frame(name = c('Bob', 'Greg', 'Mike', 'Andy'),
start = c(4, 7, 12, 16),
end = c(12, 11, 8, 22),
shift_type = c('early', 'mid_day', 'mid_day', 'late')
)
data
# name start end shift_type
#1 Bob 4 12 early
#2 Greg 7 11 mid_day
#3 Mike 12 8 mid_day
#4 Andy 16 22 late
Ggplot2 का उपयोग करके एक गैंट चार्ट बनाने के लिए जो प्रत्येक कार्यकर्ता के प्रारंभ और समाप्ति समय की कल्पना करता है, हम निम्नलिखित कोड का उपयोग कर सकते हैं:
#install (if not already installed) and load ggplot2 if(!require(ggplot2)){install.packages('ggplot2')} #create gantt chart that visualizes start and end time for each worker ggplot(data, aes(x=start, xend=end, y=name, yend=name, color=shift_type)) + geom_segment()
यह निम्नलिखित गैंट चार्ट तैयार करता है:
लेआउट में कुछ बदलावों के साथ, हम इस गैंट चार्ट को और बेहतर बना सकते हैं:
ggplot(data, aes(x=start, xend=end, y=name, yend=name, color=shift_type)) + theme_bw()+ #use ggplot theme with black gridlines and white background geom_segment(size=8) + #increase line width of segments in the chart labs(title='Worker Schedule', x='Time', y='Worker Name')
यह निम्नलिखित ग्राफ उत्पन्न करता है:
इसके अतिरिक्त, यदि आप चार्ट में उपयोग के लिए सटीक रंग सेट करना चाहते हैं, तो आप निम्नलिखित कोड का उपयोग कर सकते हैं:
ggplot(data, aes(x=start, xend=end, y=name, yend=name, color=shift_type)) + theme_bw()+ #use ggplot theme with black gridlines and white background geom_segment(size=8) + #increase line width of segments in the chart labs(title='Worker Schedule', x='Time', y='Worker Name') + scale_color_manual(values = c('pink', 'purple', 'blue'))
यह विभिन्न शिफ्ट प्रकारों को दर्शाने के लिए गुलाबी, बैंगनी और नीले रंगों के साथ निम्नलिखित ग्राफ तैयार करता है:
कस्टम थीम का उपयोग करें
हम ggthemes लाइब्रेरी से कस्टम थीम का उपयोग करके आगे बढ़ सकते हैं।
उदाहरण के लिए, हम एक गैंट चार्ट बना सकते हैं जो वॉल स्ट्रीट जर्नल से प्रेरित थीम का उपयोग करता है:
#load ggthemes library library(ggthemes) #create scatterplot with Wall Street Journal theme ggplot(data, aes(x=start, xend=end, y=name, yend=name, color=shift_type)) + theme_bw()+ geom_segment(size=8) + labs(title='Worker Schedule', x='Time', y='Worker Name') + scale_color_manual(values = c('pink', 'purple', 'blue')) + theme_wsj() + theme(axis.title = element_text())
या हम द इकोनॉमिस्ट से प्रेरित विषय का उपयोग कर सकते हैं:
ggplot(data, aes(x=start, xend=end, y=name, yend=name, color=shift_type)) +
theme_bw()+
geom_segment(size=8) +
labs(title='Worker Schedule', x='Time', y='Worker Name') +
scale_color_manual(values = c('pink', 'purple', 'blue')) +
theme_economist() +
theme(axis.title = element_text())
या शायद पाँच तीस आठ से प्रेरित विषय:
ggplot(data, aes(x=start, xend=end, y=name, yend=name, color=shift_type)) +
theme_bw()+
geom_segment(size=8) +
labs(title='Worker Schedule', x='Time', y='Worker Name') +
scale_color_manual(values = c('pink', 'purple', 'blue')) +
theme_fivethirtyeight() +
theme(axis.title = element_text())
Ggthemes लाइब्रेरी में उपलब्ध विषयों की पूरी सूची के लिए, दस्तावेज़ीकरण पृष्ठ देखें।