Dark Mode for Visualisations Using ggdark in R - GeeksforGeeks (2024)

Table of Contents
R R R R References

Last Updated : 30 Dec, 2022

Improve

Improve

Like Article

Like

Save

Report

Being a developer Dark mode is one of the favorite options of the majority. Some consider this good for the eyes and some for cosmetic reasons. So, in this article, we are going to look at such a package in the R Programming Language which enables us to introduce different Dark themes in our visualizations.

The ggdark is a package that was built on ggplot2 to produce dark theme-based plots. The ggdark package includes dark_mode(), dark_theme_minimal(), and other functions. By applying these functions the colors of the plot are inverted to ensure the plot is visible. Install and load the required packages.

dark_mode(): Used to activate dark mode on a ‘ggplot2’ theme.

Syntax:

dark_mode(.theme = theme_get(), verbose = TRUE, force_geom_invert = FALSE)

Where,

  • .theme – ggplot2 theme object
  • verbose – print messages (default: TRUE)
  • force_geom_invert – Force the inversion of geom defaults for fill and color/colour (default: FALSE)

R

data(iris)

# Plotting the density plot

ggplot2::ggplot(iris, aes(x = Sepal.Width, fill = Species)) +

geom_density(alpha = 0.7) +

ggdark::dark_mode() +

theme(legend.position = "bottom")

Output:

Dark Mode for Visualisations Using ggdark in R - GeeksforGeeks (1)

Plot with dark_mode()

dark_theme_gray(): This is one of the dark versions of complete themes from ‘ggplot2’ that are used to control all non-data displays.

Syntax:

dark_theme_gray(base_size, base_family, base_line_size, base_rect_size)

Where,

  • base_size – base font size
  • base_family – base font family
  • base_line_size – base size for line elements
  • base_rect_size – base size for rect elements

R

# load the default iris dataset

data(iris)

# Plotting the bar plot

ggplot2::ggplot(iris, aes(x = Sepal.Width,y=Petal.Width,

fill = Species)) +

geom_bar(stat="identity") +

ggdark::dark_theme_gray() +

theme(legend.position = "top")+

ggtitle("BarPlot using ggdark")

Output:

Dark Mode for Visualisations Using ggdark in R - GeeksforGeeks (2)

Plot with dark_theme_gray()

dark_theme_minimal(): This is one of the dark versions of complete themes from ‘ggplot2‘ that are used to control all non-data displays.

Syntax:

dark_theme_minimal(base_size, base_family, base_line_size, base_rect_size)

Where,

  • base_size – base font size
  • base_family – base font family
  • base_line_size – base size for line elements
  • base_rect_size – base size for rect elements

R

# load the default iris dataset

data(iris)

# Plotting the Points(Dot) Plot

ggplot2::ggplot(iris, aes(x = Sepal.Width,

y=Petal.Width,

color = Species)) +

geom_point() +

ggdark::dark_theme_minimal() +

theme(legend.position = "right")+

ggtitle("DotPlot using ggdark")

Output:

Dark Mode for Visualisations Using ggdark in R - GeeksforGeeks (3)

Plot with dark_theme_minimal()



`; tags.map((tag)=>{ let tag_url = `videos/${getTermType(tag['term_id__term_type'])}/${tag['term_id__slug']}/`; tagContent+=``+ tag['term_id__term_name'] +``; }); tagContent+=`
`; return tagContent; } //function to create related videos cards function articlePagevideoCard(poster_src="", title="", description="", video_link, index, tags=[], duration=0){ let card = `

${secondsToHms(duration)}

${title}
${showLessRelatedVideoDes(htmlToText(description))} ... Read More

${getTagsString(tags)}

`; return card; } //function to set related videos content function getvideosContent(limit=3){ videos_content = ""; var total_videos = Math.min(videos.length, limit); for(let i=0;i

'; } else{ let view_all_url = `${GFG_SITE_URL}videos/`; videos_content+=`

View All

`; } // videos_content+= '

'; } } return videos_content; } //function to show main video content with related videos content async function showMainVideoContent(main_video, course_link){ //Load main video $(".video-main").html(`

`); require(["ima"], function() { var player = videojs('article-video', { controls: true, // autoplay: true, // muted: true, controlBar: { pictureInPictureToggle: false }, playbackRates: [0.5, 0.75, 1, 1.25, 1.5, 2], poster: main_video['meta']['largeThumbnail'], sources: [{src: main_video['source'], type: 'application/x-mpegURL'}], tracks: [{src: main_video['subtitle'], kind:'captions', srclang: 'en', label: 'English', default: true}] },function() { player.qualityLevels(); try { player.hlsQualitySelector(); } catch (error) { console.log("HLS not working - ") } } ); const video = document.querySelector("video"); const events =[ { 'name':'play', 'callback':()=>{videoPlayCallback(main_video['slug'])} }, ]; events.forEach(event=>{ video.addEventListener(event.name,event.callback); }); }, function (err) { var player = videojs('article-video'); player.createModal('Something went wrong. Please refresh the page to load the video.'); }); /*let video_date = main_video['time']; video_date = video_date.split("/"); video_date = formatDate(video_date[2], video_date[1], video_date[0]); let share_section_content = `

${video_date}

`;*/ let hasLikeBtn = false; // console.log(share_section_content); var data = {}; if(false){ try { if((loginData && loginData.isLoggedIn == true)){ const resp = await fetch(`${API_SCRIPT_URL}logged-in-video-details/${main_video['slug']}/`,{ credentials: 'include' }) if(resp.status == 200 || resp.status == 201){ data = await resp.json(); share_section_content+= `

`; hasLikeBtn = true; } else { share_section_content+= `

`; } } else { share_section_content+= `

`; } //Load share section // $(".video-share-section").html(share_section_content); // let exitCond = 0; // const delay = (delayInms) => { // return new Promise(resolve => setTimeout(resolve, delayInms)); // } // while(!loginData){ // let delayres = await delay(1000); // exitCond+=1; // console.log(exitCond); // if(exitCond>5){ // break; // } // } // console.log(loginData); /*if(hasLikeBtn && loginData && loginData.isLoggedIn == true){ setLiked(data.liked) setSaved(data.watchlist) }*/ } catch (error) { console.log(error); } } //Load video content like title, description if(false){ $(".video-content-section").html(`

${main_video['title']}

${hideMainVideoDescription(main_video['description'], main_video['id'])}

${getTagsString(main_video['category'])} ${(course_link.length)? `

View Course

`:''} `); let related_vidoes = main_video['recommendations']; if(!!videos && videos.length>0){ //Load related videos $(".related-videos-content").html(getvideosContent()); } } //show video content element = document.getElementById('article-video-tab-content'); element.style.display = 'block'; $('.spinner-loading-overlay:eq(0)').remove(); $('.spinner-loading-overlay:eq(0)').remove(); } await showMainVideoContent(video_data, course_link); // fitRelatedVideosDescription(); } catch (error) { console.log(error); } } getVideoData(); /* $(window).resize(function(){ onWidthChangeEventsListener(); }); $('#video_nav_tab').click('on', function(){ fitRelatedVideosDescription(); });*/ });

Dark Mode for Visualisations Using ggdark in R - GeeksforGeeks (2024)

References

Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5873

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.