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.
R
# Install required package
install.packages
(
"ggplot2"
)
install.packages
(
"ggdark"
)
# Load the installed packages
library
(ggplot2)
library
(ggdark)
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:
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:
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:
Plot with dark_theme_minimal()
${secondsToHms(duration)}
${getTagsString(tags)}
'; } 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(); });*/ });